-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);
} 
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
  • 1
    Does it tell you which line the Syntax Error is on? – gen_Eric Oct 17 '13 at 15:59
  • 1
    You're missing parentheses around the `if isGoodEnough(x, guess)` in the `maybe` function. – Andrew Whitaker Oct 17 '13 at 16:00
  • You should use `return -1*n`, not `return -n`. Also, [JSlint](http://www.jslint.com/) is your friend. – Bucket Oct 17 '13 at 16:01
  • @DesertIvy: `return -n;` is valid code. http://jsfiddle.net/Buqh9/ – gen_Eric Oct 17 '13 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. – Rob W Oct 17 '13 at 16:02
  • possible duplicate of [How to find Javascript syntax errors](http://stackoverflow.com/questions/2120093/how-to-find-javascript-syntax-errors) – Rob W Oct 17 '13 at 16:03

6 Answers6

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.

Barmar
  • 741,623
  • 53
  • 500
  • 612
2

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

steveukx
  • 4,370
  • 19
  • 27
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));
  }
}
Default
  • 16,020
  • 3
  • 24
  • 38
  • This is true, but misspellings will cause a ReferenceError at most, not a SyntaxError. – Rob W Oct 17 '13 at 16:00
  • 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. – user1937976 Oct 17 '13 at 16:14
1

You missed the brackets

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

should be

 if (isGoodEnough(x, guess)){
    return guess;
  }
exussum
  • 18,275
  • 8
  • 32
  • 65
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.)

RichieHindle
  • 272,464
  • 47
  • 358
  • 399
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);
  }
Ignacy Debicki
  • 437
  • 4
  • 18