Why is the final value undefined for the for loop?

I am new to JavaScript and as I was practising I was not able to figure out why does the last value in my oddPairs array returns undefined when the loop ends?

const people = 56;
let numberGenerator = 0
let tickets = [];
let evenPais = [];
let oddPairs = [];

for (let counter = 1; counter <= people; counter++){
    let numberGenerator = Math.ceil(Math.random() *56);
    tickets.push(numberGenerator);
}

for (let i = 1; i <= people; i++){
    if (tickets[i] % 2 === 0){
        evenPais.push(tickets[i]);
    }else {
        oddPairs.push(tickets[i]);
    }
}

console.log(evenPais);
console.log(oddPairs);
console.log(evenPais.length + oddPairs.length);

61 thoughts on “Why is the final value undefined for the for loop?”

  1. Try this. Array always starts with 0 and ends with n – 1.

    const people = 56;
    let numberGenerator = 0
    let tickets = [];
    let evenPais = [];
    let oddPairs = [];
    
    for (let counter = 1; counter <= people; counter++){
        let numberGenerator = Math.ceil(Math.random() *56);
        tickets.push(numberGenerator);
    }
    
    for (let i = 0; i < people; i++){
        if (tickets[i] % 2 === 0){
            evenPais.push(tickets[i]);
        }else {
            oddPairs.push(tickets[i]);
        }
    }
    
    console.log(evenPais);
    console.log(oddPairs);
    console.log(evenPais.length + oddPairs.length);
    Reply
  2. ch341a programmer software 2020 download drawing program website to buy Autocad software pes 2020 for pc. office software free download for windows 7 2020 software for mac engeeeneringu#$sssaunnplus , snagit 2020 free software key. software windows 2020, best free antivirus software 2020 for windows 10 software equity group 2020 free flipbook software 2020. home office software free school office software free download, office software developer.

    Reply
  3. unsecured loans in sa i need loan now [url=https://ineedloan.me/#]need loan[/url] get a personal loan with low credit score. unsecured debt consolidation loans for poor credit uk getting a home loan steps, personal loans greenville nc. how to get a loan from online [url=https://gymfitnesspro.com/10-lies-and-truths-about-losing-fat/#comment-20666]unsecured bank loans for poor credit like[/url] 2dd2_e4 unsecured loan vpbank, unsecured bad credit personal loans direct lenders, local cash loan companies. unsecured loans journal entry get a school loan, how to get a loan with poor credit score ncb unsecured loan interest rates. unsecured personal loans low credit score quick loans like dave, interest on unsecured loan tds section personal loans meaning. best place to get a loan with low interest rates local installment loans.

    Reply

Leave a Comment