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.

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.

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

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.

I think that is what is throwing me off… I was expecting ONLY Two variables in the output. Rather than an output that had at least those 2 numbers.

lol Idk… I passed it so I’ll take that as a win. Going to a coding bootcamp in January and just trying to learn as much as possible before I start.

Maybe if we changed the function to return the values the tests will make more sense?

function functionWithArgs(param1, param2) {
  return param1 + param2;
}
console.log(functionWithArgs(1, 2) === 3);
console.log(functionWithArgs(7, 9) === 16);

The return value is the next Chalenge.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.