Why:HelperFunction-monitor's cost-saving the value


#1

At this page,
https://www.codecademy.com/programs/522912ef246eb1f1d7bd3a2dc1679dd5/content-items/e1d9909fd714abc6521483f7c384ef9a/exercises/return-ii

I answered was all checked green but reference error. So far:

function monitorCount(rows, columns) {
return rows * columns;
}

function costOfMonitors(rows, columns){
return monitorCount(rows,columns)*200;
const totalCost = costOfMonitors(5,4);
}
console.log(totalCost);

Thank you very much!


By putting console.log(totalCost); inside {} it became undefined:slightly_smiling_face
But, I still think console.log(totalCost); is outside the {}.

Thank you very much!


#2

yes, you are right. The logging should happen outside the function body. Same applies to the function call (which executes the function), this should be outside the function body also. Don’t forget what you learned about scope

why do you have the function twice?


#3

Thank you very much for answering me!

As this practice is for preparation to learn about a value came from one function is used inside another function, first of all, those two functions are written separately.
In the first function named monitorCount, the return value is how the number of monitor is counted.
Because a return produces output, and the following function can take it on with the identifier, the total cost is calculated by the number of monitors times 200(given number) in the next.
While that calculation has described, we need to make sure it in console. Perhaps, as I saw some people in stackoverflow or somewhere else put console.log(); inside a function are commented out that line for the convenience of checking. This may be for the same thing.


#4

oops, they are two separate functions

By default, a function returns None at the end of the function. If we want to return something else at the end of the function, we can use the return keyword.

Given return is the last thing a function does, when a return keyword is reached, the function ends

so what you want to do is still what i suggested, calling costOfMonitors outside/after the function.


#5

Thank you very much for the corrections.

I’ve misunderstood that the return value was like id in DOM. To be honest, I wonder why return is needed because we can access the value of a function using the identifier that has stored it in it. I’m not sure when I should use return.

And, yes, if calling costOfMonitors outside/after the function was not just for convenient to check the code so far, putting/removing the comment brackets.


#6

no? return is very different from id

true, we can. But programming is just not just about getting the solution, you also need the solution to be clean, easy to change and have reusable components, functions help with that. and return helps with passing around data in functions


#7

Yes, where the value is now and into what the value has become are very difficult to follow.


#8

costOfMonitors calls the monitorCount function

then at the end of your program, you call the costOfMonitors function and log the result