Search code examples
javascriptpseudocodecode.org

Logic Issue in Coding Activity


After look at it over with my group, we are still having trouble figuring out why it won't output the number that we type into the "textInput".

var gpa = "minimumGPA";
textInput("minimumGPA", "Enter Minimum GPA");
setPosition("minimumGPA", 55, 145, 200, 30);
setText("minimumGPA", "");
onEvent("startButton", "click", function() {
  gpa = getNumber("minimumGPA");
});
console.log(gpa);

In this pseudo code, the "startButton" is an ID that works, and "getNumber" is supposed to get the number out of the textbox from the "textInput".

Anyone with experience in code.org, what's wrong with the logic of my code?


Solution

  • "startButton" needs to be defined as a button, that you will be clicking.

    button("startButton","Start");

    Also, what you seem to be looking for is the value within "minimumGPA" text field, when you click the button. console.log(gpa) therefore needs to be within the click handler, like so

    onEvent("startButton", "click", function() {
      gpa = getNumber("minimumGPA");
      console.log(gpa);
    });
    

    Here is the complete example (with a minor change to blank out the text, when you click in it)

    var gpa = "minimumGPA";
    button("startButton","Start");
    textInput("minimumGPA", "Enter Minimum GPA");
    setPosition("minimumGPA", 55, 145, 200, 30);
    onEvent("minimumGPA","click",function() {
      setText("minimumGPA","")
    })
    onEvent("startButton", "click", function() {
      gpa = getNumber("minimumGPA");
      console.log(gpa);
    });