skip to Main Content

I wanted to make simple multiplication from 2 variables that I collected before by prompt() and I performed every check to see if anything is wrong but everything in logs seems fine. But as soon as I want to make this multiplication it throws out NaN.

I tried logging every input i got and everything was fine.

let kurs1 = 0;
let stawka1 = 0;
//function stawka() {
//    const stawka1 = parseFloat(prompt("Podaj stawkę."));
//    document.getElementById("p1").innerHTML = "Stawka wynosi: " + stawka1 + "zł.";
//}
function druzyny() {
  let druz = prompt("Podaj drużynę pierwszą.");
  let druz2 = prompt("Podaj drużynę drugą.");
  document.getElementById("druzyna1").innerHTML = "Drużyna pierwsza: " + druz;
  document.getElementById("druzyna2").innerHTML = "Drużyna druga: " + druz2;
}

function stawka() {
  const stawka1 = parseInt(prompt("Podaj stawkę."));
  if (isNaN(stawka1)) {
    console.log("Błąd: Stawka to nie liczba.");
    return;
  }
  console.log("Stawka:", stawka1);
  document.getElementById("p1").innerHTML = "Stawka wynosi: " + stawka1 + "zł.";
}

function kurs() {
  const kurs1 = parseInt(prompt("Podaj kurs."));
  console.log("Kurs:", kurs1);
  document.getElementById("kurs").innerHTML = "Kurs wynosi: " + kurs1 + "x";
}

function mozlwygr(stawka1, kurs1) {
  const wygrana = stawka1 * kurs1;
  console.log(wygrana)
}

function mozlwygr(stawka1, kurs1) {
  if (isNaN(stawka1) || isNaN(kurs1)) {
    console.log("Błąd: Stawka lub kurs to nie liczba.");
    return;
  }
  let wygrana = stawka1 * kurs1;
  document.getElementById("mozliwawygrana1").innerHTML = "Możliwa wygrana: " + wygrana + "zł.";
}
<button id="butt-wyjdz" type="button" class="btn btn-info" onclick="stawka()">Ustaw stawkę</button>
<button id="butt-kurs" type="button" class="btn btn-info" onclick="kurs()">Ustaw kurs</button>
<button id="butt-kurs" type="button" class="btn btn-success" onclick="mozlwygr()">Oblicz możliwą wygraną</button>
<button type="button" class="btn btn-secondary" id="liveAlertBtn" onclick="losuj(), mozlwygr()">Przewiduj</button>
<button id="butt-druz" type="button" class="btn btn-info" onclick="druzyny()">Drużyny</button>
<script src="gamb1.js"></script>
<script src="scriptsite.js"></script>

2

Answers


  1. You need to understand what is a variable scope !

    In JavaScript, scope refers to the accessibility or visibility of variables and functions in different parts of the code. It defines where variables or functions can be used or accessed within the code.

    Also I suggest to write your code in english !

    this is a working version of your code translated in english

    let odds1 = 0;
    let wager1 = 0;
    
    function teams() {
      let team1 = prompt('Enter the first team.');
      let team2 = prompt('Enter the second team.');
      document.getElementById('team1').innerHTML = 'First team: ' + team1;
      document.getElementById('team2').innerHTML = 'Second team: ' + team2;
    }
    
    function wager() {
      wager1 = prompt('Enter the wager.');
      if (isNaN(wager1)) {
        console.log('Error: The wager is not a number.');
        return;
      }
      console.log('Wager:', wager1);
    }
    
    function odds() {
      odds1 = parseInt(prompt('Enter the odds.'));
      console.log('Odds:', odds1);
    }
    
    function possibleWinnings(wager1, odds1) {
      const winnings = wager1 * odds1;
      console.log(winnings);
    }
    
    function possibleWinnings() {
      console.log(typeof odds1);
      console.log(typeof wager1);
      if (isNaN(wager1) || isNaN(odds1)) {
        console.log('Error: The wager or odds are not a number.');
        return;
      }
      let winnings = wager1 * odds1;
      console.log('Possible winnings: ' + winnings + 'zł.');
    }
    
    

    odds1 and wager are global variable, this menas you can access these variables in any other function,

    In your code, within the ‘wager’ function, you are redeclaring variables inside the function. This means that the variables are local, so when you save the value, you are saving it in a new variable that only exists within the scope of the function.

    Additionally, when you call the possibleWinnings function, you’ve defined two arguments (wager1 and odds1). This means the function expects two arguments to be passed when it’s called. These arguments create new local variables within the function, which define a new scope inside the function. However, you want to access the global variables instead!

    To clarify:

    When you define a function with parameters like function possibleWinnings(wager1, odds1), it creates local variables with those names within the function’s scope.

    If you pass values to the function, the function will work with those passed values, and any variables with the same names outside the function (global variables) will be ignored inside the function.

    In your case, you’re likely trying to use the global variables wager1 and odds1 that are declared outside the function, but defining the function parameters creates new local versions of these variables, which overrides the global ones inside the function.

    n fact, if you check the type of the variables in your code when the possibleWinnings function is called, you’ll see they are undefined! This happens because you’re calling the function without passing any arguments. Since the function is expecting parameters but doesn’t receive them, the local variables remain undefined. However, if you access the global variables instead, everything works as expected!

    That said, it seems you might need a bit more study and practice. These are fundamental concepts, and with more effort and dedication, you’ll master them. Keep studying and pushing forward!

    Login or Signup to reply.
  2. you’re not passing the stawka1 and kurs1 variables to the mozlwygr() function when you call it. Variables not accessible in the global scope because they are defined within their respective functions.

    You have to update your code stawka() and kurs():

    function stawka() {
      const stawka1 = parseInt(prompt("Podaj stawkę."));
      if (isNaN(stawka1)) {
        console.log("Błąd: Stawka to nie liczba.");
        return null;
      }
      console.log("Stawka:", stawka1);
      document.getElementById("p1").innerHTML = "Stawka wynosi: " + stawka1 + "zł.";
      return stawka1;
    }
    
    function kurs() {
      const kurs1 = parseInt(prompt("Podaj kurs."));
      if (isNaN(kurs1)) {
        console.log("Błąd: Kurs to nie liczba.");
        return null;
      }
      console.log("Kurs:", kurs1);
      document.getElementById("kurs").innerHTML = "Kurs wynosi: " + kurs1 + "x";
      return kurs1;
    }

    With repect to above functions, you have to update mozlwygr() to:

    function mozlwygr() {
      const stawka1 = stawka();
      const kurs1 = kurs();
      
      if (stawka1 === null || kurs1 === null) {
        console.log("Błąd: Nieprawidłowa stawka lub kurs.");
        return;
      }
      
      let wygrana = stawka1 * kurs1;
      document.getElementById("mozliwawygrana1").innerHTML = "Możliwa wygrana: " + wygrana + "zł.";
    }

    These changes require function call update on HTML side also as:

    <button id="butt-wygrana" type="button" class="btn btn-success" onclick="mozlwygr()">Oblicz możliwą wygraną</button>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search