FAQ: Functions - Helper Functions

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);
2 Likes

@mtf, thank you sir!

1 Like

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.

4 posts were split to a new topic: When i logged the totalCost to the console it gave me NaN

2 posts were split to a new topic: Why i get NaN in my code result

2 posts were split to a new topic: Why my answer is 4000?

thanks for ur responce but if ur carring the value from one function to another /helper function/ how would u define it ?

Functions talk to each other through the parameters and return values.

const isInt = (x) => Number.isInteger(x) 

const addInts = (a, b) => {
    if (isInt(a) && isInt(b)) {
        return a + b
    }
}

console.log(addInts(6, 7))        //  13

console.log(addInts(0.6, 0.7))    //  undefined
1 Like

qq
i called the function but there is no result why?

I still don’t get when and why we use variables with fuctions?

There is a result but it needs to be either assigned to a variable or logged out to the console.

a = costOfMonitors(5, 4)

console.log(a)    //  4000

Variables let us use the same function over and over but with different values being passed in.

When we passed the arguments, 5 and 4, inside the function those values are known by their parameter name. So, rows is 5, and columns is 4.

We can call the function again with different arguments and the function will act upon those values. Say we have 6 rows and 5 columns…

console.log(costOfMonitors(6, 5))  //  6000
1 Like

Is it must for a function to have RETURN statement to be called inside another function?

No, but its really common. return hands back data, which can be really useful

Screen Shot 2020-09-09 at 22.37.56
I don’t understand where is wrong.

have you tried calling costOfMonitors to see what output you get?

Yes! It is undefined.

Is that a line break after return?

Yes, it was a line break! Thank you very much!

I’m a little confused as to why we need the rows and columns data in both of the functions.

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

So - we definite a function which gives us the Count of monitors - and it does this by multiplying the columns by the rows. Makes perfect sense.

We then have a function which contains the cost of monitors, but it also requires the number of rows and columns. This makes less sense to me. We already know the number of monitors - surely the function of costOfMonitors is to take this known value and multiply it by the cost - the new information here. Why does costOfMonitors need input about the monitorCount, which it has nothing to do with?

The below works, and seems a more comprehensible solution to me:

function monitorCount(rows, columns) {
return rows*columns; //generates total number of monitors
}

let monitorCost = 200

const totalCost = monitorCount(5,4) * monitorCost;
console.log (totalCost);

Is there anything wrong with that?