Debugging Functions

The program below plans a dinner for 10. Its function, amountFor, is meant to scale one ingredient of a recipe written for 4 servings to any number of servings. This lesson finds the program's errors and repairs them one at a time.
The recipe calls for 3 cups of rice. The function was written first to print its answer, and the fourth line tests it with the recipe's own 4 servings.
function amountFor(cups, servings) {
  console.log(cups * 4 / servings);
}
amountFor(3, 4);
console.log("Cook " + amountFor(3, 10) + " cups of rice");
Output
Press Run to see what this program prints.

Note: This IDE is for experimenting with the code in this lesson. Your changes are not saved, and refreshing the page puts back the original lesson code.

It prints three lines:
3
1.2
Cook undefined cups of rice
Run the program above to see those three lines. As the lesson finds each error, repair it in the program and run it again.

A logic error

The test printed 3, the recipe's own amount, so the function looked right. The dinner call printed 1.2, less rice for 10 people than for 4.
That 1.2 could come from the arguments or from the formula. An output statement added as the body's first line, console.log("got", cups, servings), prints got 3 4 before the 3 and got 3 10 before the 1.2, so the arguments arrived as intended.
JavaScript wouldn't have reported a wrong argument count, since it never checks the number of arguments in a call. A missing one leaves its parameter undefined, and an extra one is matched to no parameter.
With 3 cups and 10 servings, the formula cups * 4 / servings is 3 * 4 / 10, or 1.2. It divides by the servings wanted and multiplies by the 4 the recipe serves, the reverse of what it should do. At 4 servings both formulas are 3 * 4 / 4, so the test couldn't tell them apart.
The repaired body is console.log(cups * servings / 4), and the dinner call now prints 7.5. The output statement has done its job, so it's removed.

Where a call can go

Some mistakes do stop the program with an error message. The test call comes after amountFor is declared, but because the function is declared with the function keyword, it could also sit above it. The same function stored with const amountFor = function (cups, servings) { ... } could not.

Printing instead of returning

The third line of output comes from the dinner call. JavaScript builds the string in the parentheses before console.log runs, so amountFor(3, 10) runs first. Its body prints 7.5 and ends with no return statement, so the call's value is undefined.
Joined to a string with +, undefined becomes the word undefined, printed between Cook and cups. In arithmetic, undefined gives NaN, which stands for not a number. Neither one stops the program, so a missing return shows up only as a wrong word or number further on.
The console.log in the body is an output statement like the one added earlier. Output statements show values while you look for a bug, but they don't change what a function returns, so adding more can't repair this one.
The repair is to return the amount, return cups * servings / 4, and leave the printing to the caller. The caller knows what the number is for, and its console.log already puts it in a sentence. The program now prints one line:
Cook 7.5 cups of rice

Using the returned value

The 3 is gone from the output. The fourth line still calls amountFor(3, 4), and the body still runs and returns 3, but nothing on that line uses the value.
The dinner call uses its value by printing it. The test now shows nothing, so it becomes console.log(amountFor(3, 4)), which prints 3, and it's deleted once testing is done.
Each call runs the body once. The printing version showed it: two calls printed two numbers, 3 and 1.2.

Storing a result once

The pantry holds 6 cups of rice, and the program should also print how many more cups to buy. One way calls the function on each line:
console.log("Cook " + amountFor(3, 10) + " cups of rice");
console.log(amountFor(3, 10) - 6 + " more cups to buy");
Another way stores the amount first:
let rice = amountFor(3, 10);
console.log("Cook " + rice + " cups of rice");
console.log(rice - 6 + " more cups to buy");
Both print Cook 7.5 cups of rice and then 1.5 more cups to buy, so both are correct. The subtraction needs the number itself. With the printing version of the function it would be undefined minus 6, and the pantry line would print NaN more cups to buy.
The two programs don't do the same work. The first calls amountFor(3, 10) twice, and each call runs the body, even with the same arguments, since JavaScript doesn't remember the first answer. Here the extra run costs a multiplication and a division. In a function whose body does more, storing the result once saves all of that work on every reuse.