Basic JavaScript: Passing Values to Functions with ArgumentsPassed

Tell us what’s happening:
Can anybody tell me what I have done I seem to not understanding this task

Your code so far


function functionWithArgs (param1, param2) {
sum(1, 2)
sum(7, 9)
console.log(param1, param2)
}
functionWithArgs(1, 2)

Your browser information:

User Agent is: Mozilla/5.0 (Linux; Android 9; LM-X420) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Mobile Safari/537.36.

Challenge: Passing Values to Functions with Arguments

Link to the challenge:

1 Like

Ok you have the parameters part right, what you need to do is add the two parameter together:
param1 + param2. Then call the function with two numbers (you have already done that).

I did that but still it says task not completed could it be that sum needs to be a variable

Tell us what’s happening:
Im not breaking through what am I doing wrong?

Your code so far


function functionWithArgs (param1, param2) {
var sum = a + b
 a = 1
 b = 2
var sum = a + b
a = 7
b = 9
console.log(a + b)
}
functionWithArgs(1, 2)

Your browser information:

User Agent is: Mozilla/5.0 (Linux; Android 9; LM-X420) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Mobile Safari/537.36.

Challenge: Passing Values to Functions with Arguments

Link to the challenge:

You just need to basically use this as a template:

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

notice how much more stuff is in yours?

I have merged the two threads.

@adeniyi.oye Please do not open more than one thread when asking for help with the same challenge.

1 Like

Here you are just logging the parameters, and using some non-existing sum function.

function functionWithArgs (param1, param2) {
sum(1, 2)
sum(7, 9)
console.log(param1, param2)
}
functionWithArgs(1, 2)

Here in the console.log(), you are doing the addition correctly but with the wrong variables (undeclared and unassigned variable BTW). You should be using param1 and param2 for the addition. You will have to remove the extra code you added because it will cause an error.

function functionWithArgs (param1, param2) {
var sum = a + b
 a = 1
 b = 2
var sum = a + b
a = 7
b = 9
console.log(a + b)
}
functionWithArgs(1, 2)
1 Like

hi @adeniyi.oye,

i think you got confused bout the funcWithArgs(param1, param2) and funcWithArgs(1, 2).

first the function :
funcWithArgs(param1,param2)
it’s like shopingCart(snack, drinks).

second the call function :
funcWithArgs(1, 2)
it’s like the cashier take the shopingCart item out would get
snack equal to 1 pcs, and drinks equal to 2 pcs.

like CactusWren wrote above, just do like the example, better if you have code editor and see the output in the browser console(developer tool).

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

testFun(2, 6);
testFun('im param1 value', 'im param2 value');

Hi @adeniyi.oye,

The challenge is requiring that you complete two parts of the solution

  1. Create a function called functionWithArgs that accepts two arguments and outputs their sum to the dev console.
  2. Call the function with two numbers as arguments.

You already have the function defined, which is the first part of #1 above. You need to use console.log to ouptut the sum of the two arguments passed in. And don’t forget to call your function at the end of your script, passing in two values.

I guess I should back up and ask if you are comfortable with functions and how they worK?

The general concept is this. If you have some code that you want to package up and reuse it in multiple places within a program, you can basically take that code and put it into a function.

So, for example lets say you have some formula—and I’m using something simple here just for making the example as simple as possible, but you can imagine this as being something more complex with more lines of code—that is implemented with this code:

var x = 4;
var y = 5;
console.log(x * y);

The code above is fine, but you may have to reuse this formula in many different places in your code. So without functions you’d have to write this every time you needed it , adjusting the x and y values depending on what is needed. Whenever you duplicate code and are rewriting it, or even copying and pasting it, there is a chance that you will introduce an error when duplicating it. This is where the power and flexibility of functions come along.

I can take the code above and package it as a function like so:

function multiply(x, y) {
   console.log(x * y);
}

To package the code up as a function, I used the keyword function followed by the name I want to give the function, in this case I used multiply but I could have used anything to name the function as long as it is a valid name according to JavaScript’s syntax rules. Then I took the variables I was using previously (x and y) and I just made them parameters by placing them inside of parenthesis after the function name. It can be stated that this function named multiply accepts two parameters, x and y. After that I have a code block that starts with an open curly brace { and ends with a closing curly brace }. Any code inside of the curly braces will run when I call the function. Which brings us to the question, what is “calling a function?”

When we have a function definition in our code like the one above, the code inside the curly braces doesn’t actually run when the JavaScript engine processes the lines of code that make up the function definition. Instead JavaScript just knows: hey, I have a function named multiply so if I ever see a call to it I’m going to run this code.

To actually call the function, you use the function name and “pass in” a value (or argument as they are called) per each parameter in the function definition (in general). That looks something like this:

multiply(4, 5); // Logs 20

You can see that calling a function requires you to use the function name followed by a value for each parameter inside of parenthesis. In the line above I’m passing the Number value 4 as an argument to parameter x and the Number value 5 as the argument to parameter y. When this line of code runs, conceptually, the JavaScript engine recognizes this as a function call, will look up the function and know that it should assign the two argument values to the function parameters, and then run the code block belonging to the function definition.

This makes the code more versatile because now I can vary the values I pass into the function to multiply different values:

multiply(10, 20); // logs 200
multiply(6, 6); // Logs 36
//... etc.

I hope that clarifies things and helps you to solve this challenge…

1 Like