0

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.

2 Answers 2

1

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;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you I didn't even notice the mistakes, is this what was causing the issue? The lack of brackets?
Points 1, 2 and 4 made your code not work - any of them would have broken it. It might have worked without correcting 3, but it's a bad idea to skip semicolons anyway.
Haaa! Thank you, again, for all your help!
1

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;
}

1 Comment

It would seem I need to move away from the Pythonic mindset. I didn't think it had that much of an effect on me all things considered... ;/

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.