// concise body syntax, implied "return"
var func = x => x * x;
// example function of consice body syntax
const areaOfCircle = radius => Math.PI * radius * radius;
areaOfCircle(4);
//Block body, explicit "return" needed
var func = (x, y) => { return x + y; };
const getUserChoice = (userInput) => {
userInput = userInput.toLowerCase();
if(userInput === 'rock' || userInput === 'paper' || userInput === 'scissors' || userInput === 'bomb'){
return userInput;
} else {
console.log('invalid input');
}
}
const getComputerChoice = () => {
let ranval = Math.floor(Math.random() * 4);
switch(ranval){
case 0:
return 'rock';
case 1:
return 'paper';
case 2:
return 'scissors';
case 3:
return 'bomb';
}
}
const determineWinner = (userChoice, computerChoice) =>{
if(userChoice === 'bomb'){
return 'user won';
} else if (computerChoice === 'bomb'){
return 'computer won';
}
if(userChoice === computerChoice){
return "Game was a tie."
}
if(userChoice === 'rock'){
if(computerChoice === 'scissors'){
return `user won with ${userChoice} over ${computerChoice}`;
} else if (computerChoice === 'paper'){
return `computer won with ${computerChoice} over ${userChoice}`;
}
}
if(userChoice === 'paper'){
if(computerChoice === 'scissors'){
return 'computer won';
} else if (computerChioce === 'rock'){
return 'user won';
}
}
if(userChoice === 'scissors'){
if(computerChoice === 'rock'){
return 'computer won';
} else if (computerChoice === 'paper'){
return 'user won';
}
}
}
const playGame = () =>{
let userChoice = getUserChoice('rock');
console.log(userChoice);
let computerChoice = getComputerChoice();
console.log(computerChoice);
console.log(determineWinner(userChoice, computerChoice));
}
// function decloration
function isGreaterThan(numberOne, numberTwo){
if(numberOne > numberTwo){
return true;
} else {
return false;
}
};
// this is an arrow expression
const isGreaterThan = (numberOne, numberTwo) => {
if(numberOne > numberTwo){
return true;
} else {
return false;
}
}
isGreaterThan(11, 12);
let cards = ['Diamond', 'Spade', 'Heart', 'Club'];
let currentCard = 'Heart';
while(currentCard !== 'Spade'){
console.log(currentCard);
currentCard = cards[Math.floor(Math.random() * 4)];
}
console.log('Program found a spade');
// this function increments a variable every time it is used
// functions should be returning values
// start at zero
let orderCount = 0;
const takeOrder = (topping, crustType) => {
// every time this function runs its getting bigger
orderCount = orderCount + 1;
console.log(`Order: ${crustType} crusty pizza with ${topping}`);
};
const getSubTotal = (itemCount) => {
return itemCount * 7.5;
};
takeOrder("mushrooms", "Gluten Free");
takeOrder("snosages", "Chinese");
takeOrder("Noodle", "Garlic");
console.log(getSubTotal(orderCount));
// Back tick
var kelvin = 294;
var celsius = kelvin - 273;
fahrenheit = celsius * (9/5) + 32;
fahrenheit = Math.floor(fahrenheit);
console.log(`the temperature is ${fahrenheit} degrees fahrenheit.`);
// the ` backtick will allow you to do string interpolation
// simple javascript array
let newYearsResolutions = [ 'code like a master',
'be an amazing influence and mentor',
'Work hard everyday'];
newYearsResolutions.push('Keep practicing');
newYearsResolutions.push('peanut butter');
newYearsResolutions.pop();
// other array methods
// .map() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
// .join() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join
// .slice() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice
// arr.slice([begin[, end]])
// .shift() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift
// removes the first item in the list
// .unshift() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift
// add items to the front of the list
// .concat() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat
let isLocked = false;
isLocked ? console.log('You will need a key to open the door.') : console.log('You will not need a key to open the door.');
let isCorrect = true;
isCorrect ? console.log('Correct!') : console.log('Incorrect!');
let favoritePhrase = 'Love That!';
favoritePhrase === 'Love That!' ? console.log('I love that!') : console.log("I don't love that!");
let moonPhase = 'full';
switch(moonPhase) {
case 'full':
console.log('Howl!');
break;
case 'mostly full':
console.log('Arms and legs are getting hairier');
break;
case 'mostly new':
console.log('Back on two feet');
break;
default:
console.log('Invalid moon phase');
break;
}
if (!favoritePhrase) {
console.log("This string doesn't seem to be empty.");
} else {
console.log('This string is definitely empty.');
}