-2

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);
}

1 Answers1

1

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}
Jose
  • 1,130
  • 3
  • 15
  • 25
  • Ah okay that worked! Wasn't aware you could put IFs inside WHILEs. You'd think the instructor would tell us! Thanks again Jose! – minecraftModder Apr 17 '13 at 19:20