Basic JavaScript Passing Values to Functions with Arguments

Tell us what’s happening:
Can someone please take a look at this code below for this functionWithArgs function below? I created two parameters (7 , 9) and should sum 16

Your code so far


// Example
function ourFunctionWithArgs(a, b) {
  console.log(a - b);
}
ourFunctionWithArgs(10, 5); // Outputs 5

// Only change code below this line.
function functionWithArgs(7, 9) {
  console.log(7 + 9);
}
functionWithArgs(7, 9);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.79 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/passing-values-to-functions-with-arguments/

You have it hardcoded with integers.
Compare your fx with the one provided. It uses variables a, b

Edit: sorry for the terrible explanation. But im on phone right now ^^

1 Like

When creating functions, you have to use variables so that they can be used with other arguments as well.
This means you need variables in the following line:

function functionWithArgs(7, 9) {

Just as in the example these have to be variables (these can’t start with a number) some examples:

function functionWithArgs(a, b) {
function functionWithArgs(x, y) {
function functionWithArgs(variable 1, variable 2) {

The 7 and 9 are applied when calling the function:

functionWithArgs(7, 9);

This line assigns a 7 to “a” and a 9 to “b”.

Then within the body of your function:

{
console.log(7 + 9);
}

Currently you are using hard coded values 7 and 9 and this is fine but will always result in 7+9 = 16. To make your function dynamically you should use the variables (a, b or x, y or whatever you chose in defining the function). Then this would become:

{
console.log(a + b);
}

If you make the necessary adjustments you will see that you can pass the challenge. But make sure that you really understand what is happening as well.

Good luck!

Thank you for your help and detailed explanation. This helped me immensely :grinning:

So this is not very clear. The lesson example says:

Parameters are variables that act as placeholders for the values that are to be input to a function when it is called. When a function is defined, it is typically defined along with one or more parameters. The actual values that are input (or “passed”) into a function when it is called are known as arguments.

Here is a function with two parameters, param1 and param2 :

function testFun(param1, param2) {
console.log(param1, param2);
}

Then we can call testFun :

testFun(“Hello”, “World”);

Nowhere does it say you that the argument/params “can’t be a number” and furthermore the notes on the left of the exercise are all incorrect then as well.

When ready this as a novice, you are under the assumption that you need to pass either 1,2 or 7,9 in order to complete/pass the lesson and move on.

I passed it with:

// Example

function ourFunctionWithArgs(a, b) {

console.log(a - b);

}

ourFunctionWithArgs(10, 5); // Outputs 5

// Only change code below this line.

function functionWithArgs(a, b){

console.log(a + b);

}

functionWithArgs(7, 9);

1 Like