Basic JavaScript - Passing Values to Functions with Arguments

Tell us what’s happening:
Describe your issue in detail here.

So I had some trouble writing this one, was following some others in the forum. I can certainly appreciate that the answer isn’t given. So I was wondering if someone could tell me why the solution is correct, but the testing output is what it is **see below. Although I passed the solution… I feel like I’m creating some sort of tech debt by doing it this way when the code outputs information like “19” & “NaN”.

Below is the console output that I got.

19
NaN
3
NaN
16
NaN
19
NaN

  **Your code so far**
function functionWithArgs(one,two,seven,nine) {
console.log(one+two);
console.log(seven+nine);
}
functionWithArgs(3,16);
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Passing Values to Functions with Arguments

Link to the challenge:

@JeremyLT - I followed one where you had a pretty lengthy conversation with Larry… feel free to delete the solution if it’s giving to much away. Just wanted to see if I could borrow your brain here.

@Cdub25 The instructions state the function will only receiving two arguments. That means the values for seven and nine are undefined Trying to add to undefined values gets you NaN.

1 Like
function functionWithArgs(one,two,) {

console.log(one+two);

}

functionWithArgs(3,16);

Even when I input this function, it still spits out 19. I guess I’m trying to understand how to get rid of 19 and only output 3 and 16. Regardless of it being correct if that makes sense.

What exactly do you want the output to be with respect to the one and two? You will have to be more specific. Maybe you should review this previous challenge that shows you how to do such things.

The challenge below

  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.

Since I passed the solution, I’m assuming I correctly created a function that accepted two arguments and outputs their sum. I called the function with the two numbers 3,16. Just not sure why 19 was in my output.

It is 19 because the sum of 3 and 16 is 19. When you use the + operator with two numbers, it creates the sum of the two numbers. Maybe you should review this challenge instead.

It asking me to call functionWithArgs with two numbers after I define it. So no matter what, even though the challenge want’s the output to be 3,16… will there will always be additional numbers because it takes the sum of those (2 numbers its asking for) inside of it?

All in all I completed the solution. I guess I’m just trying to understand how to complete that solution with clean code.

Naming your variables one and two might be confusing you.

What does the function look like if you start with

function functionWithArgs(firstNumber, secondNumber) {
 // .....
}
function functionWithArgs(firstNumber,secondNumber) {

console.log(firstNumber + secondNumber);

}

functionWithArgs(3,16);

Code passes but still gives me the same.

I’m not sure what “the same” means?

What do to see if you make these function calls:

functionWithArgs(1, 10);
functionWithArgs(2, 20);
functionWithArgs(3, 55);

function functionWithArgs(firstNumber,secondNumber) {

console.log(firstNumber + secondNumber);

11
22
58
3
16
11
22
58

How are you generating that from the function you last posted?

Sorry re-read that last one, I edited it.

function functionWithArgs(firstNumber,secondNumber) {
  console.log(firstNumber + secondNumber);
}

console.log("calling with 1 and 10");
functionWithArgs(1, 10);
console.log("calling with 2 and 20");
functionWithArgs(2, 20);
console.log("calling with 3 and 55");
functionWithArgs(3, 55);

gives me

calling with 1 and 10
11
calling with 2 and 20
22
calling with 3 and 55
58

I’m not quite sure what is confusing you here? What part of that output aren’t you expecting?

So I guess for this solution, why would it give me the output of 3 & 16 as well as give me those outputs multiple times.

There are tests that run when you click the Run Tests button that call your function with different arguments other than 3 and 16. That is why you see the other numbers displaying in the console.

Why do you think it wants that? These are the arguments for the function.

If you push the “Run Tests” button, freeCodeCamp’s test suite makes additional function calls.