H3770 VV027D
H3770 VV027D

Reputation: 35

How do I solve SyntaxError in chrome console?

The problem is to render a triangle like so:

##  
###
####
#####
######
#######

My code is

var Triangle = "#", IncreaserOfTheNumberOfTriangles = "#", Counter = 1

while (Counter < 8)
  console.log(Triangle)
  Triangle + IncreaserOFTheNumberOfTriangles
  Counter = Counter + 1

To me it seems fine but when I press enter after inputting the while(counter <8) bit into the chrome console I get SyntaxError Unexpected token. How can I fix this?

Side question > is there a place where I can find all the QWERTY keyboard keymaps( I think that's the word) in visual format preferably? I want to change my keys.

Upvotes: 0

Views: 111

Answers (2)

Evan Knowles
Evan Knowles

Reputation: 7511

JavaScript uses braces { and } to indicate blocks of code (your syntax looks more Python-esque, using whitespace). Also, you don't assign the results of your addition back, and you should end lines with ; characters, and your variable names don't match.

Try this instead

while (Counter < 8) {
  console.log(Triangle);
  Triangle = Triangle + IncreaserOfTheNumberOfTriangles;
  Counter = Counter + 1;
}

Upvotes: 1

user4632571
user4632571

Reputation:

Several issues with this.

1: Infinite loop due lack of enclosing braces. JS is not Python. Mere indentation will not blockify code. It tries to put braces where it thinks you intended them, but in this case, it would do so like:

while (Counter < 8) {
  console.log(Triangle)
}

2: You spelled the middle variable differently in the loop. IncreaserOfTheNumberOfTriangles vs IncreaserOFTheNumberOfTriangles. JS is case sensitive.

3: Don't skip semicolons at the end of lines. JS will try to place them where you don't, but it'll sometimes place them wrongly.

4: You need to actually assign the Triangle variable, because your version just makes it concatenate the two and write the result nowhere. Like so:

  Triangle = Triangle + IncreaserOfTheNumberOfTriangles;

Working code looks like this:

var Triangle = "#", IncreaserOfTheNumberOfTriangles = "#", Counter = 1;

while (Counter < 8) {
  console.log(Triangle);
  Triangle = Triangle + IncreaserOfTheNumberOfTriangles;
  Counter = Counter + 1;
}

Upvotes: 1

Related Questions