0

A group of me and two other people are working to make a Jeopardy game (themed around United States History questions) all in JavaScript. For our final Jeopardy screen, the two teams will each bet a certain amount of money. To prevent a team from typing in random letters for a bet (i.e typing in "hasdfhgasf" instead of an actual amount), we're trying to write an 'onEvent' command that checks to see if a bet is null. If that bet is null, then the code should come up with a message on the screen that tells them to check their bets again.

We tried using statements like, if "null" or if " " but neither of these statements works. We've worked with using getNumber and getText commands, along with just regular variable comparisons with or booleans. So far, we haven't had any luck with these methods.

Here's the group of code we're having issues with:

onEvent("finalJeopardyBetSubmit", "click", function() {
  team1Bet = getNumber("team1BetInput");
  team2Bet = getNumber("team2BetInput");
  console.log(team1Bet);
  console.log(team2Bet);
  if (getText("team1BetInput") == "" || getText("team2BetInput") == "") {
    console.log("Check bet!");
    finalJeopardyError();
  } else if ((getText("team1BetInput") != 0 || getText("team2BetInput") != 0)) {
    console.log("Check bet!");
    finalJeopardyError();
  } else if ((getNumber("team1BetInput") < 0 || getNumber("team2BetInput") < 0)) {
    console.log("Check bet!");
    finalJeopardyError();
  } else if ((getNumber("team1BetInput") > team1Money || getNumber("team2BetInput") > team2Money)) {
    console.log("Check bet!");
    finalJeopardyError();
  } else {
    console.log("Done");
  }
});

You can also check out the whole program on Code.org if you'd like to get a better look.

We expect that with the console.log commands, it should say "check bet" if the bets return as null. Instead, the code has ended up fine, and not displaying our error message, even if we type in nothing or just random letters.

Sina
  • 270
  • 1
  • 23
  • take a look at this stackoverflow post https://stackoverflow.com/questions/6003884/how-do-i-check-for-null-values-in-javascript/6003958 – FullStackEngineer Jun 05 '19 at 00:49
  • you can transform any string to a number by adding a '+' to it at the beginning. +myInput. Then you can check that. If that is null, then it's not a valid number. If that is not null, it's a string transformable to a number – Francisco Santorelli Jun 05 '19 at 00:50

2 Answers2

0

a null variable will evaluate to false. Try:

if(variable){
   // variable not null
}else{
  // variable null
}
Agoose Banwatti
  • 410
  • 2
  • 13
0

Convert the value to a Number first using Number(value) and then check for falsy values using the logical not ! operator. If they enter alphabetic characters, then calling Number('abc') results in NaN.

If a value can be converted to true, the value is so-called truthy. If a value can be converted to false, the value is so-called falsy.

Examples of expressions that can be converted to false are:

null; NaN; 0; empty string ("" or '' or ``); undefined.

The ! will change any of the falsy values above to true, so you can check for all of them with just the first if statement below.

onEvent("finalJeopardyBetSubmit", "click", function() {

    // Convert these values to numbers first
    var team1Bet = Number(getNumber("team1BetInput"));
    var team2Bet = Number(getNumber("team2BetInput"));

    if (!team1Bet || !team2Bet) {
       // Handle invalid number error
    }
    else if (team1Bet < 0 || team2Bet < 0) {
       // Handle invalid range error
    }
    else if (team1Bet > team1Money || team2Bet > team2Money) {
       // Handle insufficient funds error
    }
    else {
       // Finish game
    }
})

You can read more about the logical operators here.

Asleepace
  • 3,466
  • 2
  • 23
  • 36