Short-term memory ... getting unexpected token "("


#1


https://www.codecademy.com/courses/close-the-super-makert/0/3?curriculum_id=506324b3a7dffd00020bf661#


Hello fellow students ... does this error message mean that I've got extra parentheses somewhere ?


var cashRegister = {
    total: 0,
//insert the add method here    
    add: function(itemCost){
    this.total += itemCost;
    },
};
    
    scan: function (item) {
        switch (item) { 
        case "eggs": 
            this.add(0.98); 
            break;
        
        case "milk": 
            this.add(1.23); 
            break;
        
        //Add other 2 items here
        case "magazine":
            this.add(4.99);
            break;
        
        case "chocolate":
            this.add(0.45);
            break;
        
        }
        return true;
    }
};

//Scan 2 eggs and 3 magazines
cashRegister.add("eggs");
cashRegister.add("eggs");
cashRegister.add("magazine");
cashRegister.add("magazine");
cashRegister.add("magazine");
//Show the total bill
console.log('Your bill is '+cashRegister.total);


#2

Not necessarily, it means one was encountered in a situation where it is not allowed. You may just as well have something wrong in front of it.

Unexpected as in, based on what has been read so far, this does not make sense


#3

Hmm ... thank you ...


#4

What I generally like about your feedbacks ionatan is you don't necessarily give away solution but you do give away clues and hints lol .....


#5

Well, it's not.. hints, it's just that I won't draw any conclusions or ask what isn't asked and then answer myself. The point of exercises is to figure out how to solve them, not to aquire the solution.
So ask away, but consider what it is you want to know.

Asking also shows having worked on it, you wouldn't be hitting the submit button of your post if by defining the problem and formulating a question you find that you already know the answer.


#6

Yeah you're right. Having to work on it is for my own good.


#7

So what's a good way to find out which one it is?


#8

Well ... logically start to take each one of them off. One at the time and run the code ....


#9

But I don't think it's the right way to go 'coz I used the same syntax in previous exercises and passed and suddenly there's a problem ....


#10

What if there are a thousand of them?

For this code size, or while editing, proper formatting goes a long way. If I have my editor re-indent your code, the result is:

var cashRegister = {
  total: 0,
  //insert the add method here    
  add: function(itemCost){
    this.total += itemCost;
  },
};

scan: function (item) {
  switch (item) { 
    case "eggs": 
      this.add(0.98); 
      break;

    case "milk": 
      this.add(1.23); 
      break;

      //Add other 2 items here
    case "magazine":
      this.add(4.99);
      break;

    case "chocolate":
      this.add(0.45);
      break;

  }
  return true;
}
};

//Scan 2 eggs and 3 magazines
cashRegister.add("eggs");
cashRegister.add("eggs");
cashRegister.add("magazine");
cashRegister.add("magazine");
cashRegister.add("magazine");
//Show the total bill
console.log('Your bill is '+cashRegister.total);

And it should be a whole lot more evident when eyeballing it. A second syntax problem also becomes apparent.

But if there are many ('s?
Syntax errors are trivial, a machine can find them and report where they are.


#11

ionatan thanks for the walkthrough. I gotta head to work now will work on it when I get back. Thank you !


#12

Maaan ... I had "};" on the top of my code. Basically what was happening there ist the program run the code as two separate pieces instead of one ...

I compared my code with the other student's code and then it had hit me.


#13

Just about any tool that reads code will be aware of where in the code it is when it encounters something it can't parse (invalid syntax). All they have to do is count the lines it reads and print that out.
And also googling something like "how to find javascript syntax error"


#14

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.