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 / 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 / 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 / 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 / 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 / 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 / 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 / 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 / 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 / appendSum.md
Last active June 17, 2021 04:15
Take the last two numbers from an array, sum them, and append to the end of the array.

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]))
@tripdog
tripdog / truthyOrFalsy.md
Last active June 17, 2021 04:08
If the variable is truthy return true, else return false

If the variable is truthy, return true. If it is falsy return false.

function truthyOrFalsy(random) {
    return (random ?  "true" : "false") 
  
}
console.log(truthyOrFalsy(39))