Modified Function rectangleArea

I tried to modify the code in 6/11 of the Functions lesson in the basic learn JavaScript course. The code was illustrating how the js will not read code following the return function. I tried to modify it to change this as a learning challenge but I cannot seem to get anything to print to the console. Below is the original code followed by my modification. Any suggestions would be greatly appreciated.
Thanks!

Original Code:

function rectangleArea(width, height) {
  if (width < 0 || height < 0) {
    return 'You need positive integers to calculate area!';
  }
  return width * height;
}

My modification:

function rectangleArea(width, height) {
  if (width < 0 || height < 0) {
    console.log('You need positive integers to calculate area!');}
   else if (width > 0 || height > 0) {
  return width * height;
  }
}
rectangleArea(8, 7);

You have rectangleArea(8, 7); at the end
but if you wnat that to print to the console,
you may want to do console.log( that stuff );
like this;
console.log(rectangleArea(8, 7));

Or, you could store the result of the function call in a variable, and then use the variable.

let area = rectangleArea(8, 7);
console.log(area);

Also, what should be returned if width and height are both 0 ?

1 Like

Thank you for your reply. I appreciate it. I should definitely change the code to reflect (width >= 0 || height >= 0)

I think you’re saying that my code works, it simply isn’t being logged to the console. In order to call my function to the console I need console.log.

Ok, so my question then is, why is my return function not working when my last line of code:
rectangleArea (8, 7); calls the function? Shouldn’t that run the whole block of code and cause (return width * height) to run?

Updated code:

function rectangleArea(width, height) {

if (width < 0 || height < 0) {

console.log('You need positive integers to calculate area!')

}

else if (width >= 0 || height >= 0) {

return

rectangleArea(width * height);

}

}

rectangleArea(8, 7);

Thanks!

You have all of that inside the function.

Inside the function, you should have:

    return width * height;

all on one line.

You should not have:

    return rectangleArea(width * height);

inside the rectangleArea function here.
(With that code, you’d get a function calling itself over an over with no end … an infinite recursion.)

Ok,
Thank you again. Very last question, I promise. Why is the code not printing the return width * height; code? The Codecademy lesson said that the return function should print the function call to the console. Thank you again for your help and patience.

Return values tend not to be printed, since that’s not what they’re for. They internally give their value to an external thing that requests it:

function add(x,y) {
  return x + y;
}

add(1,2); // value of 3 is returned but you're not doing anything with it
console.log(add(1,2)); // the value of the function call (3) is logged to the console

There are exceptions to the “return doesn’t print” rule, such as when nothing else is logged to the console. See below.

(function defined previously)

rectangleArea(8, 7);

56

We didn’t log anything to the console, but we did call a function that returns a value, so the console printed that.

console.log(rectangleArea(8, 7));

56
undefined

Here we called console.log() on the value of rectangleArea(8, 7), which logs the value 56 to the console. console.log() does not, however, have a return value. That’s why we get a second output in console - undefined.

rectangleArea(8, 7);
console.log(rectangleArea(8, 7));

56
undefined

Here we get the same result as above, but this time it’s because the first line’s return value is ignored. It can be ignored because we already have something to output to the console - the 56 value that is logged, then the undefined value from the console.log() call itself, just like above.

1 Like

Thank you very much! That makes my understanding much clearer.

1 Like