1

I'm trying to write a simple program with JavaScript that takes in a number 1 - 100 and displays it with it's letter grade as compared to the American standard of grading. (<50 = F, 60 - 69 = D, 70 = 79 = C, etc...). I am having an issue where the script doesn't register the "STOP" command I put into it and instead, takes it as an invalid input. I want to use "STOP" to break out of the loop and any other NaN entry to throw an Error. Here is my code:

        var entry;
    var letterGrade;

    while(true) {
        entry = prompt("Enter number grade from 0 through 100\n" +
                       "Or enter STOP to end entries", "STOP");
        entry = parseInt(entry);

        // derive and display the letter grade
        if(isNaN(entry)){
            if(entry === "STOP"){
                letterGrade = 'Q';
            }
            else{
                letterGrade = 'N';
            }
        }

        else if(entry < 0 || entry > 100){
            letterGrade = "ERROR";
        }
        else if(entry < 60){
            letterGrade = 'F';
        }
        else if(entry > 59 && entry < 70){
            letterGrade = 'D';
        }
        else if(entry > 69 && entry < 80){
            letterGrade = 'C';
        }
        else if(entry > 79 && entry < 90){
            letterGrade = 'B';
        }
        else{
            letterGrade = 'A';
        }

        if(letterGrade == 'Q'){
            break;
        }
        else if(letterGrade == 'N'){
            alert("ERROR #002: Entry is not a number");
        }
        else if(letterGrade != "ERROR"){
            alert("Number grade = " + entry + "\n"
                + "Letter grade = " + letterGrade);
        }
        else{
            alert("ERROR #001: Invalid Entry");
        }
    }
1
  • you reuse the entry variable, which makes for the error here. Once you replaced its contents by using parseInt(), it value is either a number or NaN. So entry === "STOP" will always be false, hence letterGrade never by Q and you never go to the break. Commented Sep 3, 2015 at 17:08

2 Answers 2

4
entry = prompt("Enter number grade from 0 through 100\n" +
                       "Or enter STOP to end entries", "STOP");
        entry = parseInt(entry);

You can't have it both a string value and an integer. Since you reassign entry as an int (or attempt to):

if(isNaN(entry)){
            if(entry === "STOP"){

Will never match. You need to rearrange things as such:

 var entry;
var letterGrade;

while(true) {
    entry = prompt("Enter number grade from 0 through 100\n" +
                   "Or enter STOP to end entries", "STOP");

    // derive and display the letter grade
    if(isNaN(entry)){
        if(entry === "STOP"){
            letterGrade = 'Q';
        }
        else{
            letterGrade = 'N';
        }
    }
    entry = parseInt(entry); // parse to int here
    if(entry < 0 || entry > 100){
        letterGrade = "ERROR";
    }
    else if(entry < 60){
        letterGrade = 'F';
    }
    else if(entry > 59 && entry < 70){
        letterGrade = 'D';
    }
    else if(entry > 69 && entry < 80){
        letterGrade = 'C';
    }
    else if(entry > 79 && entry < 90){
        letterGrade = 'B';
    }
    else{
        letterGrade = 'A';
    }
    if(letterGrade == 'Q')
       break;
}
Sign up to request clarification or add additional context in comments.

Comments

0

It would be far better practice to use a recursive function here:

function enterGrade() {
    // All your other code above and below next line as it is above
    if(!isNan(entry)) enterGrade();
}

I can add a bit more code if you don't get what I'm trying to explain.

3 Comments

That is invalid code syntactically, you don't have any curly brackets.
Why would recursion be better here? What is this code supposed to accomplish?
It is bad practice to use while(true), this code calls a function from within the function as an alternative form of looping. If you want to end the loop, just don't call the function again.

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.