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>
3
This is a working version of your code translated in English. odds1
and wager
are global variables, which means you can access these variables in any function.
In your code, within the ‘wager’ function, you are also declaring 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!
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ł.');
}
<button id="butt-wager" type="button" class="btn btn-info" onclick="wager()">Set Wager</button>
<button id="butt-odds" type="button" class="btn btn-info" onclick="odds()">Set Odds</button>
<button id="butt-winnings" type="button" class="btn btn-success" onclick="possibleWinnings()">Calculate Possible Winnings</button>
<button type="button" class="btn btn-secondary" id="liveAlertBtn" onclick="randomize(), possibleWinnings()">Predict</button>
<button id="butt-teams" type="button" class="btn btn-info" onclick="teams()">Teams</button>
3
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>