why is my while loop returning 'undefined' in the first loop instead of 'time'?
let i = 1,
quantity;
while (i <= 10) {
console.log(`${txtsoobin.firstName} sang blue hour ${i} ${quantity}.`);
i ;
if (i == 1) {
quantity = 'time';
} else {
quantity = 'times'
}
}
sorry if the code looks messy i just started learning javascript
CodePudding user response:
Because when you declare quantity, you are not assigning any value to it, thus it is undefined
.
During the first loop, your are logging quantity before you assign it 'time'
or 'times'
. So quantity is still undefined.
If you want it to log something else than undefined, just move it below the if/else loop, like this :
let i = 1,
quantity;
while (i <= 10) {
i ;
if (i === 1) {
quantity = 'time';
} else {
quantity = 'times'
}
console.log(`${txtsoobin.firstName} sang blue hour ${i} ${quantity}.`);
}