Some Simple Questions from a Complete Beginner

Hello everyone, my name is Etienne.

So I’m teaching myself JavaScript from various sources and by doing various exercises. I feel like I’m progressing ok but I’m at a point now where, although I can make it through the exercises, there’s a lot I don’t understand. I want to secure my foundations before moving forward.

I’ll be asking my questions here in this thread. If you do want to help, please try to explain as simply as possible. I really am struggling.

So I’m working on this exercise online:
(Removed, new user)
Picture 1

The exercise is about conside body.
From what I understand, the conside body is the most concise version of the arrow form.

(removed new user)
Picture 2

#1: My first question was… what’s a parameter?
So in Picture 1, day would count as just one parameter because there’s only one value in between the brackets, am I right?

image
Picture 3
I just don’t get the return function. I don’t understand its purpose. Could someone give a real life example as an equivalent?

Hey @etiennezizka, welcome to the forums. It looks like Discourse (our forum software) automatically removed your images because you’re new.

If you’re trying to display code, I suggest putting them in code blocks instead of pictures.

Use this icon to create a code block: 05%20PM

Hello @etiennezizka,
we could say that a parameter is a variable used to pass information to a function. The function than can take that information and use it.
Also a function can have more than one parameter, depending on how it has been defined.

The “return” statement allows you to stop the execution of a function and give back a value to whatever called it. If you don’t use, when all the code in the function is run, you won’t give back any value to the caller.

Let’s assume that you define a function like this:

let sum = function (firstNumber, secondNumber){
  let result = firstNumber + secondNumber;
  return result;
}

and let’s say you call it in this way:

let whatIsMyValue = sum(3, 4);

If the return statement is there, the variable whatIsMyValue will have a value of 7, because during the execution of sum, the function will give back the result value.

If the return statement is omitted, in this case the sum function will not give back any value in the point where it has been called. So whatIsMyValue will be undefined.

Also, firstNumber and secondNumber are parameters for the sum function.

I hope it helps :slight_smile:

Simone

1 Like

Your mom can’t find her car keys, and asks you to search the house with her.

momsPocket[] = findAnItem("keys");

function findAnItem(item) {

You may have to search through the drawer, the closet, the kitchen table.

  if (item in drawer) {
     return item;
  }
  if (item in closet) {
     return item;
  }

When you get to the closet, you find the keys. You give the keys to your mom. You don’t go an check the table afterward, cause you’re done.

  if (item in table) {
     return item;
  }
  return "";
}

(the above is NOT a good example of code, but it explains the point of both parameters and returns)

1 Like

Are you familiar (conceptually) with mathematical functions, such as sine and cosine? In mathematics we might say:

  • s = sin(x)
  • c = cosine(y)

In those examples, x and y are the arguments (sometimes called parameters; I won’t get into the details of the differences between arguments and parameters) and s and c are the computed values. In computer programming terminology, the computed value is called the return value and is the value specified in the return statement.

Computer functions have a few differences from mathematical functions. Computer functions can have zero or more arguments. The return statement specifies a return value but also serves the purpose of ending the execution of the function. Computer functions might not return a value and in that case the return statement does not specify a value. If a function returns a value then that value is used in whatever expression that calls the function.

1 Like

Hello and thank kindly for your help.

So is a parameter a type of variable such as var let & const?
In that snippet of code:


Is (d) a parameter? If not? What is it called? I’m trying to get a clear picture of the terminology in my head.

But why would you want to “stop” it? “Stop” it in what way?

let sum = function (firstNumber, secondNumber){
  let result = firstNumber + secondNumber;
  return result;
}

So I would be right to think that firstNumber and secondNumber are parameters then.

I don’t understand how you embed the variable result within the variable sum.

I’m still confused but at least I got what the parameters were!

momsPocket[] = findAnItem("keys");

In that case, is “keys” a value or a parameter? I thought it was a string value. Am I wrong?

I’ll try to take things a bit at a time not be drowning in new info. I have read the other replies and will reply shortly.

@SamuelCalifornia Alas I am not familiar with that. It’s therefore hard for me to understand.

When it comes to function arguments and parameters, you might find that the middle of this post helps to reveal some good details.

1 Like

Thank you but I think this explanation is too advanced at my current level unfortunately.

Let us say that you have some code that you need to use many times. Let us say the code uses two values and produces one value. The code is the same but the variables used by the calculation are different. You can copy the code to a function and give names for the input values and then the result would be returned. Then you could simply delete the code from every place you use it and instead call the function. The call to the function can replace all the code that was the same code except for the names.

Any variable passed as a parameter to a function can be used within the function the same as if the variable was created locally within the function.

It is difficult to understand why we want to stop a function if you have no logic in the function. In very many instances, there are multiple conditions. So for example if you have a function that returns the number of words within a string, if the string is empty then you might check for an empty string and if it is empty then just return with the value zero. If the string is empty then there is no need to proceed.

2 Likes

I understand until I get to the part not in bold. What do you mean ‘give names for the input values’? That’s what I don’t get. I mean, I’m not sure.
Are you talking about this:

let sum = function (firstNumber, secondNumber){
  let result = firstNumber + secondNumber;
  return result;
}

Which part is the input values in that case?

So it’s a form of abstraction.

Think of it this way;

A function definition is the blueprint for a car. It says to the assembly machines “put an engine in, put 4 wheels on, etc”

It doesn’t tell you WHAT engine to put in. It also hasnt actually built any cars yet - it’s just told the machines what to do when someone does build a car.

Later on, someone decides they want to build a car. They then tell the robots to run their programming, and tell them that the engine is the BigBlockEngine sitting on the palette, and to use the Kingstone wheels.

(This, by the way, is also the difference between a Parameter and an Argument that was mentioned earlier - the Parameter tells the function ‘there will be an engine, and here’s what you do with it. We’ll call it ‘engine’ in the instructions.’ (function buildCar(engine,wheels)), and the Argument is what is given to the function when you tell it to run, which contains the actual information it should operate on (buildCar('BigBlockEngine','Kingstone'))

1 Like

Your example really did help a bit. I’m bookmarking this place to re-read the comments over and over.

Meanwhile I’ve decided to go back to the beginning and redo all the exercises which weren’t clear to me. I feel like right now my foundations are too shaky what I’m being told about.

The following is probably easy for you guys but I’m having trouble with it.

So the exercise is about incrementing a number in JavaScript. You know, adding +1 to a number.

image
So I need to change the code below the line and use ++ on myVar.

So I thought this would be the answer:

var myVar = 87;

// Only change code below this line
myVar = (87++);

But it’s not, it says there’s a mistake. So I checked the help:
*You can easily increment (add one) to a number variable by using the ’++’ increment operator. For example: *
var a = 6; a++; // Now, ‘a’ is equal to 7 — post-fixing ++a; // Now, ‘a’ is equal to 8 — pre-fixing
emphasized text

So my variable is value is 87. In other words, var a = 87 (changed to fit the example).
So should it just be: myVar = 87++?

But it’s not. What am I missing here?

I apologize for being lazy and not providing samples. Part of the problem is that I am not as experienced with JavaScript as others are and if I provide sample code then someone is likely to criticize it.

I am saying that if you have the following in code:

console.log(a+b);

Then the input is a and b. I am being general; there is not a formal specification here.

And if somewhere else you have:

console.log(something+somethingelse);

Then the input is something and somethingelse. Then I am saying that you can copy the code that does something+somethingelse and make it a function. Of course this sample is so simple that it seems ridiculous to make it a function but I am sure you understand that this is just a sample. So the function might be:

function sum(firstNumber, secondNumber) {
  return firstNumber + secondNumber;
}

There are often multiple ways to do things and I am using a different syntax but others will use whatever they prefer.

And then I could use:

console.log(sum(another, stillanother);

When the amount of code is greater than what we have then making a function is more useful.

See language agnostic - What’s the difference between an argument and a parameter? - Stack Overflow. You can see why I avoided defining the terms. There are very many versions of the definitions.

So the following is my definition and I expect there to be variations. I think the difference is the point of view; for the user (caller) the values are arguments; for the function the values are parameters. So someone might say the function/procedure/method signature but that seems vague to me. My version of that is to say the user (caller).

Let us use the following:

var myVar = 87;
console.log(myVar++);
console.log(++myVar);

You will get 87 and 89. That is because the postfix operator, as in myVar++, returns (assigns) the value then increments the value (to 88). The prefix operator, ++myVar, first increments the value then returns the incremented value.

1 Like

Because of the above issues with prefix and postfix, many style guides recommend to use clearer code instead.

var myvar = 87;
// ....
myvar = myvar + 1;
// or
myvar += 1;

Frequently the simple way is easy to understand, and making things easier to understand is ultimately beneficial.

1 Like

It depends on whether the style is intended to support beginners or to provide experienced programmers the freedom to program. The issue here (postix increment and prefix increment) is a very beginner thing. Experienced programmers might head for the exit when told they are expected to avoid the postfix and prefix operators.

1 Like

You will get 87 and 89. That is because the postfix operator, as in myVar++, returns (assigns) the value then increments the value (to 88). The prefix operator, ++myVar, first increments the value then returns the incremented value.

Then how I can get to 88 by using the ++ operator? I don’t understand.

var myVar = 87;
console.log(myVar++);//This will give 87
console.log(++myVar);//This will give 89

Why would myVar++ in this case get to 87?
If I’m at 87 and I add one, this should get me to 88, not 87.

If var = 6++ equals to 7, how come var = 87++ doesn’t give 88? To me it’s the exact same thing which is why I don’t understand. What am I missing here? I just change the values of a function to get two entirely different results.

What do you get with

var myVar = 87;
myVar++;
console.log(myVar);
1 Like

Hey! That’s the right answer.
I did some tests and the console.log command isn’t actually necessary here.
So this means that I don’t need to use a number here. Since I’ve already assigned 87 to my variable, I can just type in myVar++ and I’ll get to 88.

If you just type in:
myVar++
What would that be call however? I mean the line of code itself? Is it a function call?

Thanks a bunch to all of you guys!

1 Like

No, it’s not a function call. As far as I’m aware it’s just an increment; I’m not sure what else you would call it.

1 Like