Can you give us an example of this? By that i mean a code sample
Questions about the helper functions assignment. The assignment asks us to 'return monitorCount(rows, columns) and multiply by 200.
function monitorCount(rows, columns) {
** return rows * columns;**
}
function costOfMonitors(rows, columns){
** return monitorCount(rows, columns) * 200;**
}
const totalCost = costOfMonitors(5,4);
console.log(totalCost);
If I was working through this logic from scratch, what is the indicator that I need to multiply this by 200 in order to get my result?
Thank You!!
const f = (a, b) => a * b
const g = (a, b) => f(a, b) * 200
const t = g(5, 4)
console.log(t) // 4000
Must helper functions have the same number of parameters as the first function?
There is no need for the helper function to have any parameters matching the calling function. Any number of functions might well be calling the same helper function from those many contexts. That’s what makes them helper functions. They help every caller. What they offer is utility, a purpose based function that takes something and does something predictable with it and returns a result. It may be predicate or it may be an object, but on the whole, generic.
what about these lines of code:
function area(length, width) {
return length * width;
}
function volume(height) {
return area(length, width) * height
}
const newArea = area(2, 5);
const newVolume = volume(6);
console.log(newArea);
console.log(newVolume);
I’ve been messing around a bit with, and i can’t seem to get it to calculate a the volume. It tells me that the length has not been defined. Can you help me out?
Inside the function, length and width are not defined. There are two missing parameters.
We can tackle this in a number of ways, either by adding parameters and or pre=computing one value (area) and passing it with height
into the volume function.
function volume(area, height) {
}
newArea = area(2, 5)
newVolume = volume(newArea, 6)
OMG, thank you so much
Hi all,
Just a minor question:
In the exercise, we are asked to call a function and then before displaying its output, we need to assign a const variable to the output.
Why not display the result directly from the function?
Is there a reason behind this logic, or is it simply a matter of personal taste?
Code below:
const totalCost = costOfMonitors(5, 4);
console.log(totalCost); // these 2 lines are as requested in exercise to display the result
console.log(costOfMonitors(5, 4)); //isn't this more simple though?
Personal taste is subjective. Coding is objective. We write with purpose in mind.
If all we wish to do is display the outcome, then print the call expression so the return value is displayed. If we are going to need the return value at some later time or elsewhere in the code, then we would assign it.
Most of the examples and exercise are written for clarity and practice. Try not to read any style guide rules into the picture, just yet. That will come up once you start working with a team and collaborating on projects.
what’s difference between helper function and closure function?
The two terms relate to entirely different concepts.
-
helper function => a small utility that performs a simple task in predictable fashion
-
closure => the function body is a form of closure since it encapsulates its variables
A slightly more advanced form of closure that is completely self-contained is the IIFE (Immediately Invoked Function Expression)…
(function () {
// code body
})()
That function cannot be called. It is invoked immediately upon being reached by the interpreter, and only runs the one time. We have no access to any of the internals.
i thought we were not supposed to use a semi-colon after the curly brackets. In the example given it is not so. Is it a misplaced semi-colon? or am i the one not in the knowledge loop? Someone, please inform me. Thanks.
function multiplyByNineFifths(number) {
return number * (9/5);
};
function getFahrenheit(celsius) {
return multiplyByNineFifths(celsius) + 32;
};
getFahrenheit(15); // Returns 5
Good eye! They are indeed superfluous in that setting. We never write semi-colons after function declarations, as a general rule of thumb.
There is a mental test we can do if we need to ask whether one should use an end-of-statement token, or not…
Can the line that follows be scrunched up to the end of this line and not raise a syntax or runtime error?
If so, then no semi-colon needed.
While we wouldn’t do it in source listings, it is common to see minimized JS in production code that faces the web.
The above minimized…
function multiplyByNineFifths(number){return number*(9/5)}function getFahrenheit(celsius){return multiplyByNineFifths(celsius)+32}getFahrenheit(15)
No ;
needed.
Bottom line, if one learns where semi-colons are standard in JS, then C, Java and PHP will be easier to learn, as statements are concerned.
By the ES5 syntax, the above would be written,
function multiplyByNineFifths(number) {
return number * (9/5);
}
function getFahrenheit(celsius) {
return multiplyByNineFifths(celsius) + 32;
}
getFahrenheit(15);
function multiplyByNineFifths(number) {
return number * (9/5);
};
function getFahrenheit(celsius) {
return multiplyByNineFifths(celsius) + 32;
};
getFahrenheit(15); // Returns 59
What I don’t get about this exercise is why are we making two-step calculation for it?
Can’t I just summarise everything into one?
Eg.
function multiplyByNineFifths(number) {
return number * (9/5)+32;
};
The example isn’t the greatest, but breaking the program into smaller steps/separate functions can be very helpful to easier test your code and make it more readable
So the concept taught here, is really useful.