Passing Values to Functions with Arguments - Javascript


#1

Hi guys, I’m stuck and frustrated since 1 day. I m not able to figure out what s wrong in my code.

Assignment
-functionWithArgs should be a function (checked)
-functionWithArgs(1,2) should output 3 (UNchecked)
-functionWithArgs(7,9) should output 16 (checked)
-Call functionWithArgs after you define it. (checked)

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

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

functionWithArgs(3,16);

THANKS A LOT


#2

Right. Let’s break your assignment point by point:

  1. functionWithArgs should be a function.

Here you have it right, you simply need to define a function, let’s do this:

function functionWithArgs() {
//nothing here yet
};
  1. functionWithArgs should output 3.

This tells us, clearly, that our function should take two arguments, because we have (1,2) part, so let’s expand on our first example above:

function functionWithArgs(a,b) {
// nothing here yet
};

In addition to this, it also tells us what the output should be. Instead of thinking about it now, let’s move to 3rd point that will help us see the pattern.

  1. functionWithArgs(7,9) should output 16.

And here we stop for a moment. Take a look at points 2 and 3. This is the same function (have the same name, additionally same number of parameters). What do they have in common? If we think of a very simple operations, you can notice that the output of this function is nothing else, but a simple addition of arguments:
1 + 2 = 3
7 + 9 = 16

So, what should you do now? You should define body of your function to do just that.

I am not sure what’s the context of this task / assignment (the last point confuses me), but your functionWithArgs(a,b) should either return or print (console.log()) your function output.

function functionWithArgs(a, b) {
  // here you place your function body, e.g. console.log(a);
};

// and here we call this function with parameters a = 1, b = 2, which should output 3
functionWithArgs(1,2);

Hope this makes it a bit easier to understand now?


#3

thank you it s working now,

function functionWithArgs(a, b) {
console.log(a + b);
}

functionWithArgs(1,2);
or
functionWithArgs(7,9); same!!!