I recently was making a little script that would ask for tv series titles and logs them on the console in an array. I used a keyword to stop the loop which is 'terminate' and it should not be included, however, the following code adds the keyword to the array.
//-----------------firts case code ------------------//
let lista = [];
let series;
function collect(){
while (series !== 'terminate'){
series = prompt('Add a tv show');
lista.push(series);
}
}
collect();
console.log(lista);
More confusing still, is that in the next piece of code, I managed to make the script to leave the keyword out of the array.
//---------------------------Second case code--------------------------//
let lista2 = [];
let series;
while (true){
series2 = prompt('add tv serie');
if (series2 !=='terminate'){
lista2.push(series2);
}else{
break;
}
}
console.log(lista2);
My question is, how the use of the boolean as the expression to evaluate in a loop affects the result as opposed to the first case scenario with the first case code?