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
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
odds1
andwager
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
andodds1
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 remainundefined
. 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!
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():
With repect to above functions, you have to update mozlwygr() to:
These changes require function call update on HTML side also as: