Question

Question, how do I make it so when an user-inputted number is negative, it isn't added to the total variable that will be ouput?

Code below!

function lab10logicInLoopsPart1() 
{

lCounter = 1;
var total = 0;
userNumber = 0;

while(lCounter < 6) {
lCounter++;
userNumber = prompt("Enter a number.");
total += +userNumber;
document.write("Entered number was: " + userNumber + "\n");
}
document.write("\nTotal: " + total);
}
Était-ce utile?

La solution

After prompting the user to enter a number, just check it's not a negative with an if statement:

if(userNumber >=0)
{ //do your stuff here}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top