1.

Solve : Problem getting loop to restart on error Javascript?

Answer»

I am trying to get my loop to go back to the beginning on the first error.

Code: [Select]{
    VAR validInput = true;
    var start = confirm('Add item to shoping cart');
    if (start == true) {

        // ask first question
        var orderProductCodeArr = parseInt(prompt('Enter input: '), 10);
 
        if (isNaN(orderProductCodeArr)) {
            alert("input is not a valid number");
            validImput = false

        } else if (orderProductCodeArr < 0 || orderProductCodeArr &GT;= PRODUCT_LIST.length) {
            alert("code does not match any item");
            validInput = false;
        }
 
        // ask SECOND question

         else if(validInput == true) {
            var item = PRODUCT_LIST[orderProductCodeArr];
            alert("item is: " + item);
}
            // get quantity input
           
            // this is pseudocode
var quanityArr = parseInt (prompt('Enter quality amount'),10);
            if (isNaN(quanityArr)) {
            alert("input is not a valid number");
            validInput = false;

        }
       
       
     
       
    } else {
        document.writeln('still to come')
    }
 
}

  Quote from: sonya1m on April 14, 2013, 10:08:03 PM

I am trying to get my loop to go back to the beginning on the first error.

 It has been pointed out to me that I have no loop so I guess I NEED to ask, how do I make it into one loop first before I get to my next question.

Code: [Select]{
    var validInput = true;
    var start = confirm('Add item to shoping cart');
    if (start == true) {

        // ask first question
        var orderProductCodeArr = parseInt(prompt('Enter input: '), 10);
 
        if (isNaN(orderProductCodeArr)) {
            alert("input is not a valid number");
            validImput = false

        } else if (orderProductCodeArr < 0 || orderProductCodeArr >= PRODUCT_LIST.length) {
            alert("code does not match any item");
            validInput = false;
        }
 
        // ask second question

         else if(validInput == true) {
            var item = PRODUCT_LIST[orderProductCodeArr];
            alert("item is: " + item);
}
            // get quantity input
           
            // this is pseudocode
var quanityArr = parseInt (prompt('Enter quality amount'),10);
            if (isNaN(quanityArr)) {
            alert("input is not a valid number");
            validInput = false;

        }
       
       
     
       
    } else {
        document.writeln('still to come')
    }
 
}

 


Discussion

No Comment Found