How to import variables from one function to another in javascript?

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>

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật