Skip to content

Instantly share code, notes, and snippets.

Avatar
💭
meditating

Tom Phillips tripdog

💭
meditating
View GitHub Profile
@tripdog
tripdog / rockPaperScissor.md
Last active Jun 21, 2021
Write a Rock, Paper, Scissors Game
View rockPaperScissor.md

Create a rock, paper, scissors game that you play against the computer.

const items = ["rock", "paper", "scissors"]
function game(userInput) {
    let computerInput = items[Math.floor(Math.random() * items.length)]
    console.log(computerInput)
    if (computerInput === userInput) {
        return "This is a tie"
    } else if (computerInput === "rock") {
@tripdog
tripdog / wordReverse.md
Created Jun 19, 2021
Create a function called `wordReverse` that reverses a string using built-in JS methods
View wordReverse.md

Create a function called wordReverse that reverses a string using built-in JS methods

const wordReverse = (string) => {
    return string.split("").reverse().join("")
}
console.log(wordReverse("This is how we do it."))

//.split creates an array=['T', 'h', 'i', 's', ' ',  'i', 's', ' ', 'h', 'o', 'w', ' ', 'w', 'e', ' ', 'd', 'o', ' ', 'i', 't', '.']
//.reverse simply reversese the array
//.join puts the elements of the string back together in a string
@tripdog
tripdog / forEach.md
Last active Jun 19, 2021
Write a function that takes an array of numbers add 10 to every element of the array using forEach( ) and returns a new array.
View forEach.md

Write a function that takes an array of numbers and adds 10 to every element of the array using the forEach( ) method and returns a new array.

const anotherArr = [ 12, 11, 12, 13, 14 ]
let addToArr = []
anotherArr.forEach(num => {
   addToArr.push(num + 10)
})
console.log(addToArr)

//Logs [ 22, 21, 22, 23, 24 ]

@tripdog
tripdog / greetAliens.md
Created Jun 17, 2021
Create a function called greetAliens that taks an array and adds a greeting to the aliens
View greetAliens.md

Create a function called greetAliens that taks an array and adds a greeting to the aliens.

function greetAliens(aliens) {
    let greeting = []
    for (let i = 0; i < aliens.length; i++) {
      greeting.push("Greetings " + aliens[i]);
    }
    return greeting
}
console.log(greetAliens(["SpaceKing", "Wegord", "Klingon", "Blorgous", "Vulcan"]));
@tripdog
tripdog / getSum.md
Created Jun 17, 2021
Create a function that takes in a list and find the sum of all the numbers
View getSum.md

Create a function that takes in a list and find the sum of all the numbers.

const arr =[1, 2, 3]

function getSum(list) {
  let sum = 0;
  for (let i = 0; i < list.length; i++) {
    sum = sum + list[i];
  }
  return sum;
@tripdog
tripdog / squareNums.md
Created Jun 17, 2021
Create a function that returns the square of each number in an array.
View squareNums.md

Create a function that returns the square of each number in an array.

function squareNums(arr) {
    const newArray = []
    for (let i = 0; i < arr.length; i++){
     newArray.push(arr[i] * arr[i]);
    }
    return newArray
}
console.log(squareNums([2,4,6,8,10]));
@tripdog
tripdog / babies.md
Created Jun 17, 2021
Create a function that loops through an array adding the word "baby" to each item.
View babies.md

Create a function that loops through an array adding the word "baby" to each item.

const convertToBaby = (animals) => {
    const newArray = []
    for (let i = 0; i < animals.length; i++){
        newArray.push('baby ' + animals[i])
    }
    return newArray;
}
@tripdog
tripdog / reverseArray.md
Last active Jun 17, 2021
Write a function that takes an array and returns a new array that's a reverse of the oringinal.
View reverseArray.md

Write a function that takes an array and returns a new array that's a reverse of the oringinal.

function reverseArray(arr) {
    const newArr = []
    for (i = arr.length - 1; i >= 0; i--){
        newArr.push(arr[i])
    }
    return newArr
}
console.log(reverseArray([3, 4, 5]))
@tripdog
tripdog / maxNum.md
Created Jun 17, 2021
Write a function that returns the biggest number from an array
View maxNum.md

Write a function that returns the biggest number from an array.

function maxNum(arr) {
    let max = 0
    for (let i = 0; i < arr.length; i++){
        if (arr[i] > max ) {
            max = arr[i]
        }
    }
    return max
@tripdog
tripdog / appendSum.md
Last active Jun 17, 2021
Take the last two numbers from an array, sum them, and append to the end of the array.
View appendSum.md

Take the last two numbers from an array, sum them, and append to the end of the array.

function appendSum(nums) {
    nums.push(nums[nums.length - 1] + nums[nums.length - 2]); 
    nums.push(nums[nums.length - 1] + nums[nums.length - 2]); 
    nums.push(nums[nums.length - 1] + nums[nums.length - 2]);
    return nums
}
console.log(appendSum([1, 1, 2]))