-
-
Save Avi-E-Koenig/fcf18f498008e1fdd84196b11703921d to your computer and use it in GitHub Desktop.
Triangle one loop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var triangleSize; | |
// Keep prompting for a valid positive number | |
while ( | |
isNaN(triangleSize) || | |
typeof triangleSize !== 'number' || | |
triangleSize < 3 || | |
triangleSize % 1 != 0 | |
) { | |
triangleSize = parseInt( | |
prompt('Enter the middle line of the triangle (a positive number 3 or higher):') | |
); | |
} | |
var triangle = '\n'; | |
var count1 = 0; /**determine when to line break */ | |
var count2 = 1; | |
var directionForward = true; /**determine when to extend or shorten lines*/ | |
var line = ''; | |
while (count2 != 0) { | |
line += '*'; | |
count1++; | |
if (triangleSize === count2) { | |
directionForward = false; | |
} | |
if (count1 === count2) { | |
directionForward ? count2++ : count2--; | |
count1 = 0; | |
triangle += line + '\n'; | |
line = ''; | |
} | |
} | |
console.log('🚀 ~ file: loops.js:284 ~ triangle', triangle); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For a use case where the render loop can only have one loop and no methods within