TGR
TGR

Reputation: 3

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;}

Upvotes: 0

Views: 50

Answers (1)

Jack Bashford
Jack Bashford

Reputation: 44135

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;
}

Upvotes: 1

Related Questions