admin管理员组

文章数量:1122846

I’m preparing for a coding bootcamp and working through JavaScript exercises as part of the application process.

I’ve been tasked with:

Write a function called addNums that will add 1 and 2 in an expression and assign the result to a variable named mysteryNumber.

Here’s the code I’ve written so far:

function addNums(a, b) {
    return a + b;
}

const mysteryNumber = addNums(1, 2);
console.log(mysteryNumber);

Issue

When I test this in the debug console, it works perfectly and logs 3. However, when I try to submit my solution, I get the following error message:

"Remember to include parentheses"

I’m not sure what part of my code this message is referring to. I’ve included parentheses in the function call and the function itself.

What I’ve Tried

  • Double-checked the syntax of the function.
  • Ensured that parentheses are included when calling the function.
  • Reviewed online examples of similar problems, but I’m still stuck.

Question

What does "Remember to include parentheses" mean in this context? Is there something wrong with how I’m calling or defining the function? Any guidance would be greatly appreciated!

I’m preparing for a coding bootcamp and working through JavaScript exercises as part of the application process.

I’ve been tasked with:

Write a function called addNums that will add 1 and 2 in an expression and assign the result to a variable named mysteryNumber.

Here’s the code I’ve written so far:

function addNums(a, b) {
    return a + b;
}

const mysteryNumber = addNums(1, 2);
console.log(mysteryNumber);

Issue

When I test this in the debug console, it works perfectly and logs 3. However, when I try to submit my solution, I get the following error message:

"Remember to include parentheses"

I’m not sure what part of my code this message is referring to. I’ve included parentheses in the function call and the function itself.

What I’ve Tried

  • Double-checked the syntax of the function.
  • Ensured that parentheses are included when calling the function.
  • Reviewed online examples of similar problems, but I’m still stuck.

Question

What does "Remember to include parentheses" mean in this context? Is there something wrong with how I’m calling or defining the function? Any guidance would be greatly appreciated!

Share Improve this question asked Nov 21, 2024 at 12:22 808Squad808Squad 115 bronze badges 6
  • 2 Your code looks fine. Perhaps the checker is expecting you to call the function as addNums(), and then have your function not accept any parameters and instead just return 1 + 2; (since the question doesn't specify the function needing to accept arguments) – Nick Parsons Commented Nov 21, 2024 at 12:26
  • 1 const is a constant, not a variable, use var or let instead. – Martin Zeitler Commented Nov 21, 2024 at 12:41
  • @MartinZeitler Thanks for your comment, I have implemented var & let but still running into errors - "Remember to declare your variable with const as it won't be reassigned" – 808Squad Commented Nov 21, 2024 at 12:44
  • @NickParsons Hey thanks for your comment, Here's the error after I implemented what you suggested - "Double check your expression - it should be adding 1 and 2" – 808Squad Commented Nov 21, 2024 at 12:46
  • 1 @MartinZeitler, per MDN, "The const declaration declares block-scoped local variables.". It's still a variable; it just doesn't (can't) change. And mysteryNumber does not change here, so const is fine. – mykaf Commented Nov 21, 2024 at 14:22
 |  Show 1 more comment

1 Answer 1

Reset to default 1

I think the task itself is kind of confusing...

I can only (logically) infer that it wants this answer:

// Write a function called addNums
function addNums ( expression )
{
  // that will add 1 and 2 in an expression
  const result = expression + 1 + 2;
  
  // and assign the result to a variable named mysteryNumber
  mysteryNumber = result;
}

// TESTING:
addNums('SomeString');
console.log(mysteryNumber);

It's still possible to reduce the code to this:

function addNums ( expression )
{
  mysteryNumber = (expression + 1) + 2;
}

// TESTING:
addNums('SomeString');
console.log(mysteryNumber);

The parentheses are optional in mysteryNumber = (expression + 1) + 2;.

PS.: I don't know if the expected answer is this (by who imputed this task), but the logic of the task requires this answer.

NOTE:

  1. The task is not requiring to test the function, so maybe you should discard anything that is not the function in your answer.

  2. The task is not requiring that the function must return the result.

  3. The expression parameter is not required by the task and is optional as its value could be defined in an outer context (for example the global context where mysteryNumber is or should be defined).

  4. I don't think that "Remember to include parentheses" is actually an error, but rather a hint generated by the system that dumbly thinks you should add parentheses to fix your answer.

  5. This answer is probably not what the task actually expects you to answer.

Here in my country, dubious tasks in tests must be eliminated on demand. For example, if a task is "write 1 + 2 in full" and you answer "one plus two", but the expected answer was "three", then you can demand to cancel that question because it's dubious, regardless if your answer was right or wrong, they must cancel the task and redistribute the points given to each task or give full marks for that task specifically... I don't know if its the case for this task or even if its feasible where you are.

本文标签: How do I fix the quotRemember to include parenthesesquot error in my JavaScript functionStack Overflow