Created
September 8, 2016 23:30
-
-
Save monicao/7f7d0ede85bbeba774fd573578878615 to your computer and use it in GitHub Desktop.
W4D3 Breakout Closures
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
/* | |
* Question: What is the difference between the two ways of declaring functions in JavaScript? | |
*/ | |
// | |
// Declaring a function with var. | |
// | |
// console.log(sayHello); // Outputs undefined because var sayHello is hoisted up. | |
sayHello() // Doesn't work. because sayHello is undefined | |
var sayHello = function() { | |
console.log("Hello"); | |
} | |
sayHello() // Works | |
// Takeaway: You cannot call a function defined with var before it is defined | |
// | |
// Declaring a function with the function keyword. | |
// | |
sayHello() // Works | |
function sayHello() { | |
console.log("Hello"); | |
} | |
sayHello() // Works |
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
/* | |
* Question: What is the difference between the two ways of declaring functions in JavaScript? | |
*/ | |
// | |
// Declaring a function with var. | |
// | |
// console.log(sayHello); // Outputs undefined because var sayHello is hoisted up. | |
sayHello() // Doesn't work. because sayHello is undefined | |
var sayHello = function() { | |
console.log("Hello"); | |
} | |
sayHello() // Works | |
// Takeaway: You cannot call a function defined with var before it is defined | |
// | |
// Declaring a function with the function keyword. | |
// | |
sayHello() // Works | |
function sayHello() { | |
console.log("Hello"); | |
} | |
sayHello() // Works |
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 readline = require("readline"); | |
var read = readline.createInterface({ | |
input: process.stdin, | |
output: process.stdout | |
}); | |
console.log("Welcome to the Math Game!"); | |
// Sample Output | |
// =================== | |
// Welcome to the Math Game! | |
// Player 1: What is 88 + 123? 211 | |
// You guessed correctly! | |
// Player 2: What is 12 + 19? 40 | |
// You guessed wrong! 2 lives left. | |
// ... eventually | |
// Player 2: What is 100 + 1? 102 | |
// YOU'RE DEAD. Player 1 for the win! | |
// Things our program will have to do | |
// generate two random numbers | |
// generate an answer | |
// accept input from the player | |
// keep track of the player's lives | |
// every time the user enters an answer and the answer is wrong, check if the game ended | |
function generateQnA() { | |
// generate two random numbers | |
var num1 = Math.floor(Math.random() * 100) | |
var num2 = Math.floor(Math.random() * 100) | |
var question = num1 + " + " + num2; | |
var answer = num1 + num2; | |
return {question: question, answer: answer}; | |
} | |
function askQuestion() { | |
// ask player1 a question | |
var question = generateQnA(); | |
console.log("Player: What is " + question.question + "?"); | |
read.question("Answer: ", function(answer) { | |
if(question.answer === parseInt(answer)) { | |
console.log("CORRECT"); | |
} else { | |
console.log("WRONG"); | |
} | |
askQuestion(); | |
}); | |
} | |
askQuestion(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment