Pseudocode for JavaScript Loop Assignment

I’m in a bit of a pickle, regarding my Pseudocode for an assignment I’m working on. It was marked incorrect saying I need to add a validation loop (which I thought I did). I’m pretty new to coding as my background is in IT Support. Any help explaining to me how to add a loop validation into my Pseudo would be much appreciated as Pseudo isn’t really taught in this course and I’m a bit lost to be honest.

//PSEUDOCODE FOR assignment1.js
//input
/*
ONCLICK.PROMPT
FUNCTION
WINDOW.PROMPT
VAR CHOICE("Which website would you like?")
WHILE true
       SWITCH (CHOICE CASE 1 - 3)
       BREAK;
ELSE alert ("please enter a valid number")
       RETURN TO FUNCTION
*/

Actual JS Script (Linked to a HTML).

function pressButton(){
    var myElement= document.getElementById("websites");
    var choice = parseInt(prompt("Which website would you like?n 1:Google n 2:Yahoo n 3:Bing n","" ));

while (choice<0 || choice>3){
            alert("please enter a valid number");

    var choice = parseInt(prompt("Which website would you like?n 1:Google n 2:Yahoo n 3:Bing n","" ));
        

switch (choice) {
    case 1:
    window.open("https://www.google.com",'_blank', height=800, width=800);
    break;

    case 2:
    window.open("https://au.yahoo.com","_blank", height=800, width=800);
    break;

    case 3:
    window.open("https://bing.com","_blank", height=800, width=800);
    break;

    default:
    text = "error: please choose from the options above";

}
}
}

Answer

The best way to follow pseudo-code is keep it with the real code. As for your issue, the loop performs the validation. Outside the loop (after it) is when a valid choice has been made. Currently your switch is inside the validation loop, which means it’ll run when the choice value is invalid. This matches your pseudo-code but unfortunatley your pseudo-code is wrong. Its a tough balance keeping the pseudo code small enough to match how a computer steps through the calculation and keeping actual code out of the pseudo text. Here’s how I’d change it.

// ONCLICK
// GET CHOICE
// WHILE CHOICE INVALID
   // ALERT OF INVALID CHOICE
   // GET CHOICE AGAIN
// OPEN CHOICE
// END FUNCTION

Other tips. Your switch doesn’t need a default unless an invalid choice can be made. Your validation should not include 0. This is also a great scenario for do…while (although that alert becomes an awkward scenario).

// ONCLICK
function pressButton(){
    var myElement= document.getElementById("websites");
    // GET CHOICE
    var choice = parseInt(prompt("Which website would you like?n 1:Google n 2:Yahoo n 3:Bing n","" ));

    // VAR CHOICE("Which website would you like?")
    // WHILE invalid
    while (choice <= 0 || choice > 3) {
        // ALERT OF INVALID CHOICE
        alert("please enter a valid number");
        // GET CHOICE AGAIN
        var choice = parseInt(prompt("Which website would you like?n 1:Google n 2:Yahoo n 3:Bing n","" ));
    }
        
    // OPEN CHOICE
    switch (choice) {
        case 1:
        window.open("https://www.google.com",'_blank', height=800, width=800);
        break;

        case 2:
        window.open("https://au.yahoo.com","_blank", height=800, width=800);
        break;

        case 3:
        window.open("https://bing.com","_blank", height=800, width=800);
        break;
    }

    // END FUNCTION
}

Leave a Reply

Your email address will not be published. Required fields are marked *