Skip to content

Instantly share code, notes, and snippets.

@eamaccready
Last active March 20, 2019 01:49
Show Gist options
  • Save eamaccready/1bd7444de819f38cb96b17702686ce86 to your computer and use it in GitHub Desktop.
Save eamaccready/1bd7444de819f38cb96b17702686ce86 to your computer and use it in GitHub Desktop.
Snippits from Chapter 4 quizzes
# Snippits from Chapter 4 quizzes
/*
* Programming Quiz: Changing the Loop (4-4)
*/
// rewrite the while loop as a for loop
/*var x = 9;
while (x >= 1) {
console.log("hello " + x);
x = x - 1;
}*/
for (var x= 9; x >= 1; x--){
console.log("hello " + x);
}
/*
* Programming Quiz: 99 Bottles of Beer (4-2)
*
* Use the following `while` loop to write out the song "99 bottles of beer".
* Log the your lyrics to the console.
*
* Note
* - Each line of the lyrics needs to be logged to the same line.
* - The pluralization of the word "bottle" changes from "2 bottles" to "1 bottle" to "0 bottles".
*/
var num = 99;
while (num > 0) {
// check value of num
if (num > 2){
// print lyrics using num
console.log(num + " bottles of beer on the wall! " + num +
" bottles of beer! Take one down, pass it around... " + (num-1) +
" bottles of beer on the wall!");
}
// don't forget to check pluralization on the last line!
else if (num === 2){
console.log(num + " bottles of beer on the wall! " + num +
" bottles of beer! Take one down, pass it around... " + (num-1) +
" bottle of beer on the wall!");
}
else{
console.log(num + " bottle of beer on the wall! " + num +
" bottle of beer! Take one down, pass it around... " +
(num-1) +" bottles of beer on the wall!");
}
// decrement num
num--;
}
/*
* Programming Quiz: Countdown, Liftoff! (4-3)
*
* Using a while loop, print out the countdown output above.
*/
var x = 60;
while(x >= 0 ){
switch(x){
case 50:
console.log("Orbiter transfers from ground to internal power");
break;
case 31:
console.log("Ground launch sequencer is go for auto sequence start");
break;
case 16:
console.log("Activate launch pad sound suppression system");
break;
case 10:
console.log("Activate main engine hydrogen burnoff system");
break;
case 6:
console.log("Main engine start");
break;
case 0:
console.log("Solid rocket booster ignition and liftoff!");
break;
default:
console.log("T-"+ x + " seconds");
}
x--;
}
/*
* Programming Quiz: Factorials (4-7)
*/
var solution = 1;
for(x = 12; x > 0; x--){
solution *= x;
}
console.log(solution);
/*
* Programming Quiz: Find my Seat (4-8)
*
* Write a nested for loop to print out all of the different seat combinations in the theater.
* The first row-seat combination should be 0-0
* The last row-seat combination will be 25-99
*
* Things to note:
* - the row and seat numbers start at 0, not 1
* - the highest seat number is 99, not 100
*/
// Write your code here
for (var i = 0; i < 26; i++){
for (var j = 0; j < 100; j++){
console.log(i +"-" + j);
}
}
/*
* Programming Quiz: FizzBuzz (4-1)
*/
var x = 1;
while (x <=20) {
if (x % 15 === 0){
console.log ("FizzBuzz");
}
else if (x % 5 === 0){
console.log("Buzz");
}
else if ( x % 3 === 0){
console.log("Fizz");
}
else{
console.log(x);
}
x++;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment