Please help explain 9 to me, urgentt!


#1

hey please i am confused why doesnt this coding work :

// Write your function starting on line 3

var perimeterBox = function (length,width) {
var val = (length + length + width + width);
console.log(val)
};

perimeterBox(6,2);

but this other coding works : ,

var perimeterBox = function(length, width) {
return length + length + width + width;
};
var newNumber = perimeterBox(10,5)

please explain i am confused as mad lol.


#2

One is using console.log and the other one is using return. Could you give us the error message you receive?


#3

the error message i got from using the console.log method is :
Oops, try again. It looks like your perimeterBox function doesn't return the correct perimeter (4) for a width of 1 and a length of 1.


#4

You need to use the return keyword. Not console.log. That's your problem.


#5

i know that lol but why is that?


#6

It doesn't want you to log the information, it wants you to return it, so it can be processed further afterwards.


#7

You want to get into the habit of returning in your function, unless specified otherwise.


#8

oh ok so when am i meant to use 'console.log' and when am i meant to use 'return' regarding variable functions?


#9

Always use return unless specified.


#10

In any case the instructions should guide you what variable names you need to choose and what way of output is required. If words like "console.log, log or print" come up it means that you should use console.log and if you're asked to return something well you need to use return. Return has actually some advantages because the returned value can be used in the program and it can be shown on the screen whereas console.log just shows it on the screen but after this the value is gone. If stuff like this is not mentioned in the instructions it mostly means that it doesn't care in this exercise and if it is missing and the program cares about it is probably a bug but I'm not aware of a case where this is happening in the JS track.


#11

oh ok thank you very much,that makes more sense


#12

thanks for the help and your time