0

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";

}
}
}

2 Answers 2

2

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
}
Sign up to request clarification or add additional context in comments.

1 Comment

Updated my answer to include better pseudo-code. Try to keep pseudo-code in english, with no actual code. Make sure it flows step by step like a computer will handle it.
1

You should not run the switch statement if the value of choice is beyond the range. There is a bug in your code which will print the message only once and then pass the incorrect values of choice to the switch statement.

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");
        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;

    }
 }

The code above will keep asking the user to provide a number until user provides the right input. Also, you don't need the default case because the while loop will make sure that only the correct value is passed on to the switch block

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.