Reputation: 1
Ive written this function for loop to display the numbers once the answer to the prompt is give, but everytime the loop appears, it displays the numbers and then the term undefined at the end. Is there a particular reason why?
function numbers(){
var i=2;
while(i<=20){
document.write("This is the number "+i+"<br>");
i+=2;
}
}
var age = 21;
var age = prompt("How old are you?");
if(age==21){
document.write(numbers());
}
Upvotes: 0
Views: 477
Reputation: 895
Because you are printing the numbers from within the function and then printing the return from the function in the line document.write(numbers());
. Since your function does not return anything, so it prints undefined
.
Upvotes: 1