Skip to content

JavaScript code error #21397

Answered by mpboom
aditikochhar asked this question in General
Discussion options

You must be logged in to vote

Hi @aditikochhar and welcome to the community!

There are several things going on here that is will cause errors:

  • You cannot use spaces in function names. Try renaming your function to  addTwo instead of  add<space>Two.
  • You re-define  x inside your function by using the  var keyword. That way, the  inside your function is a different  x than the  x in the first line.

This should fix the errors mostly, however I also do recommend to pass the variable that needs the addition as a function argument as well as giving the variable of the product a different name (for clarity). Your code will look something like this then:

var x = 1;
function addTwo(number) {
  return number + 2;

};
var y =…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants