I'm new to JavaScript (I've used python for ~3yrs, and I'm currently trying to pick up the basics of JS syntax.) I am using codewars to facilitate learning.https://www.codewars.com/kata/5bb904724c47249b10000131/train/javascript
This problem gives the prompt
function points(games) {
// your code here
}
The rules are:
if x>y -> 3 points
if x<y -> 0 point
if x=y -> 1 point
My naive approach is to create a function which receives an array as an input and apply a forEach method to every element therein. However, I'm triggering the following error: Uncaught SyntaxError: Unexpected token 'else'
games = ["0:3","1:2","3:2"]
function points(games){
let p = 0;
games.forEach(
function(game){
let x = game.split(':')[0];
let y = game.split(':')[1];
if(x>y){
p = p + 3};
else if(x=y){
p = p + 1};
else {
p = p + 0;
};
});
};
I'd like to better understand (A) why this error is beginning triggered and (B) what is the right way to accomplish this effect.
Edit: I likely need to cast x and y as a numeric type, however this isn't triggering the current error.
}in your code};<-- that is not how you do if statements. Look at the syntax on MDN developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… and you are doing assignment, not comparison.;is certainly not needed here, but it's also not "wrong": it simply does nothing. Sure, it's silly to put one in, butif (x) { ... } ;;;;;;;;;;does exactly the same asif (x) { ... }: a semi-colon in isolation is simply a noop.};you were commenting on (given that there are three).