Question: IN HTML Javascript In each instance where you ask the user how many items they want, trap them in a loop until they give you

IN HTML Javascript

In each instance where you ask the user how many items they want, trap them in a loop until they give you a whole, positive number (there is a slide in the PowerPoints for this).

PLEASE HELP. I am having issues being trapped in a loop after entering not a whole number ( example 4.5). I am able to continue when is NaN and when it is a negative. I am just trying to be able to access the next prompt statement after receiving a whole number and no longer be trapped.

This is the example code given in the power point

var bracelets = parseInt(prompt("How many bracelets do you want?"," "));

var check = bracelets % 1;

while(isNaN(bracelets) || (bracelets < 0) || (check != 0)) {

bracelets = prompt("Please enter a positive whole number. How many bracelets do you want?"," ");

}

This is my code within my first function.

function first{

var name = parseInt(prompt("Enter the amount of vinyls you would like: "));

var check = name % 1;

while(name<0 || isNaN(name) || (check !=0)) {

name=prompt("Please enter a valid amount for vinyls you would like to purchase: ");

}

var sign = parseInt(prompt("Enter 1 for regular, Enter 2 for signed copies"));

while (sign != 1 && sign != 2 ){

var sign = prompt("Please enter 1 for regular, Enter 2 for signed copies");

}

var price = parseInt(name)* 20;

var signed = parseInt(name) * 40;

if (sign ==1){

alert("Your price is $" + price); }

else if(sign==2){ alert("Your price is $" + signed) }

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!