1

ユーザー入力エラーが発生したときにループを再開しようとしています。最後の質問だけでなく、最初からやり直す必要があります。

したがって、validImput = false と表示されている場合は、ここで再起動しようとしています。

{
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


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

}

4

2 に答える 2

0

試す

function test()
{

   for(var s=0;s<5;s++)
   {
    try
    { 

     //body of the for loop

    }catch(e){s=0;}
   }

}
于 2013-04-15T05:38:15.093 に答える
0

何かを最初からやり直す通常の方法は、ある種のループ構成であり、多くの場合、while次のように使用します。

while (true) {
    // your loop code here

    // you can use break; to break out of the while loop 
    //     anywhere to stop repeating
    // you can use continue; to jump to the next iteration immediately

}

または、次のようなループ条件を使用する場合もあります。

var doAgain = true;
while (doAgain) {

    // within the loop, you set doAgain to false when you are done
    // and don't want to repeat the loop again

}
于 2013-04-15T05:58:42.890 に答える