简体   繁体   中英

I cannot add a function inside a function

I am learning JavaScript right now and I created a working function and now I want to call it inside another function but I cant

Here is my function

function findHeight1() {
  gravity = promptNum("Enter given gravity (If on earth g = 9.8)");
  time = promptNum("Enter given Time");
  height = 1/2 * (gravity * (time * time));}

I tried to add it inside a new function and it didn't work

function findPE2() {
  height = findHeight1();
  mass = promptNum("Enter given Mass");
  potentialEnergy = (mass * gravity) * height;}

You need to return height from findHeight1 - but even so, since you're declaring your variables without var , let or const , they're being created as globals:

function findHeight1() {
  let gravity = promptNum("Enter given gravity (If on earth g = 9.8)");
  let time = promptNum("Enter given Time");
  let height = 1/2 * (gravity * (time * time));
  return height;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM