2

I am having an issue on my problem where the console is saying

SyntaxError: Unexpected token {

and I'm not sure where it's coming from. I am a JS newbie, just learning this stuff. Would you mind pointing out where I am going wrong with my brackets please.

var myArray = [4, 6, 23, 10, 1, 3];

var arrayAdditon = function (arr) {
    var largestNumber = arr[0];
    var sumTotal;
    for (var i = 0; i < arr.length; i += 1) {
        if (arr[i] > largestNumber) {
            largestNumber = arr[i];
        }
    }
    for (var i = 0; i < arr.length; i += 1) {
        if (largestNumber) {
            console.log(largestNumber);
        } else (arr[i] != largestNumber) {
            sumTotal += arr[i];   
        }
    }
    if (largestNumber === sumTotal) {
        return true;
    } else {
        return false;
    }
}
5
  • 4
    Check your syntax for errors here jshint.com Commented Dec 27, 2013 at 4:58
  • Thanks for that. I am familiar with that tool, although I am not seeing where the error is. Commented Dec 27, 2013 at 5:03
  • Well, what line is the error reported? Commented Dec 27, 2013 at 5:04
  • 2
    else takes no condition, it's just else {... Commented Dec 27, 2013 at 5:05
  • BTW, JSHint got it: "14: Expected an assignment or function call and instead saw an expression." Commented Dec 27, 2013 at 5:06

5 Answers 5

5

I think you have a bug in your if...else part, try changing to this.

 if (largestNumber) {
     console.log(largestNumber);
 } else if (arr[i] != largestNumber) {
     sumTotal += arr[i];   
 }

If this doesn't work comment me the full error you got. Try to inspect it in firebug console. Hope this helps you.

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

Comments

3

Your code updated here....

var myArray = [4, 6, 23, 10, 1, 3];

var arrayAdditon = function (arr) {
var largestNumber = arr[0];
var sumTotal;
for (var i = 0; i < arr.length; i += 1) {
    if (arr[i] > largestNumber) {
        largestNumber = arr[i];
    }
}
for (var j = 0; j < arr.length; j += 1) {
    if (largestNumber) {
        console.log(largestNumber);
    } else if (arr[i] != largestNumber) {
        sumTotal += arr[i];   
    }
}
if (largestNumber === sumTotal) {
    return true;
} else {
    return false;
}
 };

Comments

1

error in syntax else (arr[i] != largestNumber) put if there

Comments

1

I can not see a missing {

but

} else (arr[i] != largestNumber) {
        sumTotal += arr[i];   
}

should be

} else if (arr[i] != largestNumber) {
        sumTotal += arr[i];   
}

Comments

0

Here is the fixed one.

var myArray = [4, 6, 23, 10, 1, 3];

var arrayAdditon = function (arr) {
    var largestNumber = arr[0],
        sumTotal, i;

    for (i = 0; i < arr.length; i += 1) {
        if (arr[i] > largestNumber) {
            largestNumber = arr[i];
        }
    }
    for (i = 0; i < arr.length; i += 1) {
        if (largestNumber) {
            console.log(largestNumber);
        } else if(arr[i] != largestNumber) {
            sumTotal += arr[i];   
        }
    }
    if (largestNumber === sumTotal) {
        return true;
    } else {
        return false;
    }
};

arrayAdditon( myArray );

Thanks to JSHint.

2 Comments

why did you do sumTotal, i'?
To declare loop counter i only once at the beginning. Then use it multiple times in for loop

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.