-1

I'm learning JavaScript by reading Eloquent JavaScript. I'm running my code on the console provided by the book's website. I'm getting a SyntaxError : Unexpect Identifier from the following bit of code. Please help.

function absolute (n){
  if (n < 0)
    return -n;
  else
    return n;
}  

function average(x, y){
  return (x + y) / 2;
}


function isGoodEnough(x, guess){
  return (absolute(x - guess) < 0.0001);
}

function maybe(x, guess){
  if isGoodenough(x, guess){
    return guess;
  }
  else{
    return maybe(x, average(x, x/guess));
  }
}

function sqrt(x){
  return maybe(x, 1);
} 
6
  • 1
    Does it tell you which line the Syntax Error is on? Commented Oct 17, 2013 at 15:59
  • 1
    You're missing parentheses around the if isGoodEnough(x, guess) in the maybe function. Commented Oct 17, 2013 at 16:00
  • You should use return -1*n, not return -n. Also, JSlint is your friend. Commented Oct 17, 2013 at 16:01
  • @DesertIvy: return -n; is valid code. jsfiddle.net/Buqh9 Commented Oct 17, 2013 at 16:01
  • 1
    This question and its answers suck... They're only useful for this very specific question. A debugging tutorial would be much more helpful to future visitors. Commented Oct 17, 2013 at 16:02

6 Answers 6

3

You're missing the parentheses in your if statement:

if (isGoodEnough(x, guess)) {

You also misspelled the function name, and that will cause a different error.

Sign up to request clarification or add additional context in comments.

Comments

2

if isGoodenough(x, guess){ is missing the parentheses: if(isGoodenough(x, guess)){

Comments

1

isGoodEnough is mispelled in the maybe function.

function maybe(x, guess){
  if isGoodenough(x, guess){
    return guess;
  }
  else{
    return maybe(x, average(x, x/guess));
  }
}

2 Comments

This is true, but misspellings will cause a ReferenceError at most, not a SyntaxError.
If fixed the brackets and the spelling error. Now I'm exceeding the maximum call stack. I'll just go rewrite it with a loop.
1

You missed the brackets

 if isGoodEnough(x, guess){
    return guess;
  }

should be

 if (isGoodEnough(x, guess)){
    return guess;
  }

Comments

1

You need parentheses around this if condition:

function maybe(x, guess) {
    if (isGoodEnough(x, guess)) {   // Note extra parentheses
        ...

(You've also misspelled "isGoodEnough" here.)

Comments

0

you need to put parantheses around the if conditions and else as well. Also use brackets to test statments after if statments. should look like

 function absolute (n){
  if (n < 0){
    return -n;
    }
  else {
    return n;
    }
  }  

 function average(x, y){
   return (x + y) / 2;
   }


 function isGoodEnough(x, guess){
   return (absolute(x - guess) < 0.0001);
   }

 function maybe(x, guess){
  if (isGoodEnough(x, guess)){
     return guess;
     }
  else{
     return maybe(x, average(x, x/guess));
     }
 }

  function sqrt(x){
   return maybe(x, 1);
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.