samedi 10 octobre 2015

Using for loop after if /else statement

This is just a simple multiplication program using an if/else and for loop. Re-entering a positive number from a prompt and assigning it to the num variable which the for loop should use is breaking the program. I’m not sure why it is incorrect.? Perhaps you could tell me the best way to do this using the if /else and for loop. Any help much appreciated.

//Array variable and counter variable

var multi = new Array();

var Counter;

//Enter a number between 1 and 12 and hold in num variable

num = prompt("Enter a number between 1 and 12");

//Check if number is less than zero
//Reenter positive number

if (num < 0){

    var num = prompt("Enter a number greater than zero")
}
    else 

    {


//Number entered by user multiplied by counter value

for (Counter = 1; Counter <= 12; Counter++)

{

     multi[Counter] = num * Counter


}


//Loop to display number that is being multiplied each time 

for (Counter = 1; Counter <= 12; Counter++)

{   

    document.write(Counter  +  " x " +  num  +  "  = "  + multi[Counter] + '<br/>')
}

}

Aucun commentaire:

Enregistrer un commentaire