My challenge with functions in Js.
Functions are a newish concept to me. I’ve been struggling to understand their basic structure for a couple of weeks now. I realized that what I was trying to do was simply memorize how to build certain functions like adding a number or returning true after a certain criteria is met. But what I felt I needed to do was take a step back and try to grasp their order, to visualize what it is that actually makes up a function in Javascript. As I understand it now, there are 3 parts to a function. The name (where you declare a function and parameter which are placeholders for input), the body (this holds the action-what the function will actually do), and the calling of the function (where you will log the function to get results or see if it works).
My first challenge after identifying the structure of functions was to check whether the sum of two numbers is greater than 10. So first, the name. isSumGreaterThan10-and I included the parameters num1 and num2 (remember that parameters are like placeholders for input that will come later on). Next, I needed to figure out the body-what will the function do? Ultimately, it needs to add 2 numbers and check if that sum is greater than 10. If it is, it will log ‘true’. Else, it will log ‘false’. In order to add the two numbers you need to store the result. To do this you need to create a variable in the body. For example, let sum = num1 + num2. After this, we need an if/else statement. This would look like if sum > 10 return true, else if sum < 10 return false. So far the code looks like this:
function isSumGreaterThan10(num1, num2) {
let sum = num1 + num2;
if (sum > 10) {
return ‘true’;
else if (sum < 10) {
return false;
}
}
Easy right? ;) Well, it wasn’t for me. After discussing the structure of functions with Chatgtp the way functions are built began to make more sense to me. But knowing what a house looks like doesn’t necessarily mean you know exactly what went on inside to build that house. I kind of new an if/else statement would be required, but I hadn’t figured on the need to store adding two numbers (the variable within). Another error I made was writing out a console.log within the body instead of return. It “worked”, and I was able to log true and false but the way Chat explained it to me is the difference between console.log and return is that the former only logs the result, and the other returns it. Meaning, when you use return you store it in a variable to use later.
So we’ve got the action (the body). Now in order to call the function (make the action happen) we need to create a variable in order to store the function and its parameters. So now our code from above is looking like this:
function isSumGreaterThan10(num1, num2) {
let sum = num1 + num2;
if (sum > 10) {
return ‘true’;
else if (sum < 10) {
return false;
}
}
let result = isSumGreaterThan10(1, 6);
and in order to log it and check if our code works we add:
console.log(result);
What I’ve described above is a basic function declaration in Javascript. Basic implies simple. And it is, but it’s not. But having discovered or identified the structure of a function they are looking less and less ‘magical’. I just gotta keep practicing!