Skip to content

Instantly share code, notes, and snippets.

@lyxal
Created July 28, 2021 02:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lyxal/344968f8f6e089990ad81b4f22002ac4 to your computer and use it in GitHub Desktop.
Save lyxal/344968f8f6e089990ad81b4f22002ac4 to your computer and use it in GitHub Desktop.
Prompt: In order to count to 10, you need to do the following:
Use a loop:
for (var count = 1; count < 10; count++) { console.log(count); }
Use a single-line if statement:
if (count < 10) { console.log(count); }
Use a while loop:
while (count < 10) { console.log(count); count++; }
Use a do-while loop:
do { console.log(count); count++; } while (count < 10);
Use a for-in loop:
for (var key in object) { console.log(key); }
Use a for-of loop:
for (let item of object) { console.log(item); }
Use a for loop:
for (var i = 0; i < 10; i++) { console.log(i); }
Use a while loop with a condition that will always be true:
var i = 0; while (true) { console.log(i); i++; }
Use the break statement:
for (var i = 0; i < 10; i++) { if (i == 3) { break; } console.log(i); }
Use the continue statement:
for (var i = 0; i < 10; i++) { if (i == 3) { continue; } console.log(i); }
Use the throw statement:
for (var i = 0; i < 10; i++) { if (i == 3) { throw new Error('Problem!'); } console.log(i); }
Use the return statement:
function range(start, stop, step) { for (var i = start; i < stop; i += step) { console.log(i); } return; }
Use the with statement:
var object = { firstName: 'Nicholas', lastName: 'Barry' }; with (object) { console.log(firstName); console.log(lastName); }
Use
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment