Last active
November 26, 2018 19:18
-
-
Save FlintSable/e5d1abcf80191b38ff3e6e696432d89a to your computer and use it in GitHub Desktop.
Javascript tricks
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
| // 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 |
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
| let secretMessage = ["Learning", "isn't", "about", "what", "you", "get", "easily", "the", "first", "time,", "it's", "about", "what", "you", "can", "figure", "out.", "-2015,", "Chris", "Pine,", "Learn", "JavaScript"]; | |
| secretMessage.pop(); | |
| secretMessage.push('to', 'program'); | |
| secretMessage[6] = 'right'; | |
| secretMessage.shift(); | |
| secretMessage.unshift('Programming'); | |
| secretMessage.splice(6, 4, 'know'); | |
| console.log(secretMessage.join(" ")); |
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
| let calculatorIsOn = false; | |
| const pressPowerButton = () => { | |
| if (calculatorIsOn) { | |
| console.log('Calculator turning off.'); | |
| calculatorIsOn = false; | |
| } else { | |
| console.log('Calculator turning on.'); | |
| calculatorIsOn = true; | |
| } | |
| }; | |
| pressPowerButton(); | |
| // Output: Calculator turning on. | |
| pressPowerButton(); | |
| // Output: Calculator turning off. |
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
| // 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; }; | |
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
| // 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)); |
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
| if (!favoritePhrase) { | |
| console.log("This string doesn't seem to be empty."); | |
| } else { | |
| console.log('This string is definitely empty.'); | |
| } |
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
| let groceryList = ['orange juice', 'bananas', 'coffee beans', 'brown rice', 'pasta', 'coconut oil', 'plantains']; | |
| groceryList.shift(); | |
| groceryList.unshift('popcorn'); | |
| console.log(groceryList.slice(1, 4)); | |
| console.log(groceryList); |
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
| // 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); |
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
| 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)); | |
| } |
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
| // 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 | |
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
| 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; | |
| } |
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
| 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!"); | |
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
| 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'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment