Skip to content

Instantly share code, notes, and snippets.

View tripdog's full-sized avatar
💭
meditating

Tom Phillips tripdog

💭
meditating
View GitHub Profile
@tripdog
tripdog / maxNum.md
Created June 17, 2021 04:32
Write a function that returns the biggest number from an array

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 / reverseArray.md
Last active June 17, 2021 04:35
Write a function that takes an array and returns a new array that's a reverse of the oringinal.

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 / babies.md
Created June 17, 2021 04:59
Create a function that loops through an array adding the word "baby" to each item.

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 / squareNums.md
Created June 17, 2021 05:07
Create a function that returns the square of each number in an array.

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 / getSum.md
Created June 17, 2021 05:10
Create a function that takes in a list and find the sum of all the numbers

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 / greetAliens.md
Created June 17, 2021 05:12
Create a function called greetAliens that taks an array and adds a greeting to the aliens

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 / forEach.md
Last active June 19, 2021 20:03
Write a function that takes an array of numbers add 10 to every element of the array using forEach( ) and returns a new array.

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 / wordReverse.md
Created June 19, 2021 22:55
Create a function called `wordReverse` that reverses a string using built-in JS methods

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 / rockPaperScissor.md
Last active June 21, 2021 05:02
Write a Rock, Paper, Scissors Game

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 / createHTMLwithJS.md
Last active July 1, 2021 20:00
Challenge: create a basic HTML page by importing a JS file into a completely blank HTML page

Master the DOM: Create a simple web page by importing a JS file into a blank HTML page

//wrapping div
const wrapper = document.createElement("div")
document.body.appendChild(wrapper);
wrapper.style.display = "flex"
wrapper.style.alignItems = "center"
wrapper.style.justifyContent = "center"
wrapper.style.width ="100%"