Skip to content

Instantly share code, notes, and snippets.

@Avi-E-Koenig
Last active February 18, 2023 23:04
Show Gist options
  • Save Avi-E-Koenig/fcf18f498008e1fdd84196b11703921d to your computer and use it in GitHub Desktop.
Save Avi-E-Koenig/fcf18f498008e1fdd84196b11703921d to your computer and use it in GitHub Desktop.
Triangle one loop
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);
@Avi-E-Koenig
Copy link
Author

For a use case where the render loop can only have one loop and no methods within

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment