0

Hey i'm learning about closure functions and JavaScript, and decided finding the volume of a rectangle might be a good way. I also want it to not do any more math if I try to put in another number after I've already calculated the area. However, the following code only returns [Function]. It seems to just ignore me doing the math on the parameters I put in.

let volume = height => {
  return function(width) {
    return function(length) {
      return height * width * length
    }
  }
}
let rec = volume(10);
rec(6)
console.log(rec(3)) //this should return 180
console.log(rec(7)) //this should still return 180
Barmar
  • 741,623
  • 53
  • 500
  • 612
Noah M
  • 33
  • 1
  • 4
  • Did you mean to write `let rec = volume(10)(6)`? – Unmitigated Apr 15 '21 at 16:53
  • variadic functions and currying don't go together. You can't get that behavior. You can make a curried function for your three parameters, but not get that added functionality you seek. – ASDFGerte Apr 15 '21 at 16:54
  • Why do you think the last should return 180? `10 * 6 * 7 == 420` – Barmar Apr 15 '21 at 16:54
  • @ASDFGerte There are no variadic functions here. – Barmar Apr 15 '21 at 16:55
  • @Barmar the desired function should "not do any more math if I try to put in another number after I've already calculated the area", as in, `volume(10)(6)(3)(7)` should stick to `180`. Maybe i misunderstood something, but i interpreted it that way. – ASDFGerte Apr 15 '21 at 16:56
  • Maybe this question is relevant: https://stackoverflow.com/questions/31306312/javascript-function-challenge-add1-2-and-add12-both-should-return-3 – Barmar Apr 15 '21 at 16:58
  • You seem to be expecting `rec(6)` to modify modify `rec` somehow. It just returns a new function, which you're discarding. – Barmar Apr 15 '21 at 18:23

3 Answers3

1

The volume function returns a function.

You assign that function to rec.

The rec function returns a function.

The first time you call rec, you ignore the return value completely.

The second and third times you call it, you log the result… where you will see a function, because the rec function returns a function.

There is no internal counter which would cause rec to return different things depending on how many times you call it.

Or, to rewrite your code with more verbose names:

let function_that_accepts_height = height => {
  return function(width) {
    return function(length) {
      return height * width * length
    }
  }
}
let function_that_accepts_width = function_that_accepts_height(10);
let function_that_accepts_length = function_that_accepts_width(6);
let final_result = function_that_accepts_length(3);
console.log(final_result);
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • I want it to run the function and find the volume as soon as rec(3) is called without storing it in a variable – Noah M Apr 15 '21 at 17:13
  • @NoahM `rec` only saves 1 dimension that came from `volume(10)`. So `rec(3)` only has 2 dimensions, you need 3 to calculate the volume. – Barmar Apr 15 '21 at 18:22
1

You need to assign rec(6) to another variable, and then call that to get all 3 dimensions multiplied.

In my code below I've renamed the variables to better express how they fit together.

let volume = height => {
  return function(width) {
    return function(length) {
      return height * width * length
    }
  }
}
let line = volume(10);
let rec = line(6)
console.log(rec(3)) //this should return 180
console.log(rec(7)) //this should return 420
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

volume(10) returns a function you call rec. rec also returns a function, does it not? So you need to call that one too to get the result of height * width * length.

I assume rec comes from "rectangle"? But when you call volume you have only passed the height. You have to call the response with a width too to get the function that now has enough data to compute the surface of a rectangle. Then you call that one with a length to get a volume.

Sergiu Paraschiv
  • 9,929
  • 5
  • 36
  • 47