I have gone through the stackoverflow questions and have found answers that are similar/close to the issue I am having, but it seems my understanding is lacking.
I have an array that consist of ages (4, 21, 47, 16, 18 etc). I am trying to sort the array so that it would find every index position that is under 18. I have writen a for loop for this, but it terminates everytime there is a age over 18.
var text = "";
for (var i = 0; i <= myArray.length && myArray[i] < 18; i++) {
text += i;
}
console.log(text);
text = ""; // with this I get answer 0 to console, where as i would want
0 3.
I have also tried following, but these will give me only the first value under 18 (or not work in general). I have just started coding so I dont understand these ways as well so I would prefer to do it with for loop if possible:
1)
function underEighteen() {
myArray.reduce(function a, e, i) {
if (e < 18)
a.push(i);
return a;
}, []);
}
2)
function isUnderEighteen(elementBelow18) {
return elementBelow18;
}
function sortAge() {
var under18 = myArray.find(isUnderEighteen);
}
I have tried several other simple ways to sort the array to no avail. Help appreciated! Javascript only...
edit: So I have a plain web page with a textbox where you can insert age&name and save them. Then I have a possibility to show everyone who are not allowed to buy alcohol and those who are (under and over 18).
My aim is to do this so that I find the index positions of those elements that are under 18, pull the same elements from name array that correspond those of the age arrays on post them on the page with:
document.getElementbyId("notAllowetoDrink").innerHTML = "Do not sell
alcohol + "XXXXXXX;