Basic JavaScript Passing Values to Functions with Arguments

Basic JavaScript Passing Values to Functions with Arguments
0.0 0

#1

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/


#2

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 ^^


#3

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!


#4

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