Java Script

CODE CHALLENGES: INTERMEDIATE JAVASCRIPT

Jassy 2020. 7. 22. 16:23

 

1. reverseArray()

const reverseArray = arr => {
  let reversed = [];
  for (let i = arr.length-1; i >= 0; i--) {
    reversed.push(arr[i])
  }
    return reversed;
}

const sentence = ['sense.','make', 'all', 'will', 'This'];

console.log(reverseArray(sentence)) 

/*
[ 'This', 'will', 'all', 'make', 'sense.' ]
*/

 

2. greetAliens() ===.forEach

const greetAliens = (arr) => {
  for (let i=0; i<=arr.length; i++) {
   console.log(`Oh powerful ${arr[i]}, we humans offer our unconditional surrender!`)
  }

}

const aliens = ["Blorgous", "Glamyx", "Wegord", "SpaceKing"];

greetAliens(aliens);

/*
Oh powerful Blorgous, we humans offer our unconditional surrender!
Oh powerful Glamyx, we humans offer our unconditional surrender!
Oh powerful Wegord, we humans offer our unconditional surrender!
Oh powerful SpaceKing, we humans offer our unconditional surrender!
Oh powerful undefined, we humans offer our unconditional surrender!
*/

 

3. convertToBaby() ===  .map()

const convertToBaby = (arr) => {
    let newArray=[]
  for (let i=0; i< arr.length; i++) {
    newArray.push(`baby ${arr[i]}`)
  } return newArray;
}


const animals = ['panda', 'turtle', 'giraffe', 'hippo', 'sloth', 'human'];

console.log(convertToBaby(animals)) 

/*
[ 'baby panda',
  'baby turtle',
  'baby giraffe',
  'baby hippo',
  'baby sloth',
  'baby human' 
  */

 

4. Fix The Broken Code!

const numbers = [5, 3, 9, 30];

const smallestPowerOfTwo = arr => {
      let results = [];
      for (let i = 0; i < arr.length; i++) {
            number = arr[i];

      let  j = 1;
      while (j < number) {
                  j = j * 2;
            }
           results.push(j);
      }
      return results
}

console.log(smallestPowerOfTwo(numbers)) 

/* [ 8, 4, 16, 32 ] */

 

5. declineEverything() and acceptEverything() ===.forEach

const veggies = ['broccoli', 'spinach', 'cauliflower', 'broccoflower'];

const politelyDecline = (veg) => {
      console.log('No ' + veg + ' please. I will have pizza with extra cheese.');
}

 const declineEverything = veg => {
  veg.forEach(politelyDecline);
}

declineEverything(veggies);

const acceptEverything = veg => {
  veg.forEach(veg=>{
    console.log(`Ok, I guess I will eat some ${veg}.`)
    })}


acceptEverything(veggies);



/*
No broccoli please. I will have pizza with extra cheese.
No spinach please. I will have pizza with extra cheese.
No cauliflower please. I will have pizza with extra cheese.
No broccoflower please. I will have pizza with extra cheese.

Ok, I guess I will eat some broccoli.
Ok, I guess I will eat some spinach.
Ok, I guess I will eat some cauliflower.
Ok, I guess I will eat some broccoflower.

*/

 

6. squareNums() === .map()

 

const numbers = [2, 7, 9, 171, 52, 33, 14]

const toSquare = num => num * num

const squareNums = numbers => numbers.map(toSquare);

console.log(squareNums(numbers));

/*
[ 4, 49, 81, 29241, 2704, 1089, 196 ]
*/

 

7. shoutGreetings()=== .map()

const shoutGreetings = arr => arr.map(word => word.toUpperCase() + '!');


const greetings = ['hello', 'hi', 'heya', 'oi', 'hey', 'yo'];

console.log(shoutGreetings(greetings))

// [ 'HELLO!', 'HI!', 'HEYA!', 'OI!', 'HEY!', 'YO!' ]

 

 

8. sortYears()=== .sort()

const sortYears = arr => 
 arr.sort((x, y) => x < y)


const years = [1970, 1999, 1951, 1982, 1963, 2011, 2018, 1922]

console.log(sortYears(years))

// [ 2018, 2011, 1999, 1982, 1970, 1963, 1951, 1922 ]

 

 

9. justCoolStuff() === .filter ()

 

const justCoolStuff = (arr1, arr2) => 
  arr1.filter(items => arr2.includes(items)) 



const coolStuff = ['gameboys', 'skateboards', 'backwards hats', 'fruit-by-the-foot', 'pogs', 'my room', 'temporary tattoos'];

const myStuff = [ 'rules', 'fruit-by-the-foot', 'wedgies', 'sweaters', 'skateboards', 'family-night', 'my room', 'braces', 'the information superhighway']; 

console.log(justCoolStuff(myStuff, coolStuff))

//  [ 'fruit-by-the-foot', 'skateboards', 'my room' ]

 

10. isTheDinnerVegan() ===

const isTheDinnerVegan = food => 
  food.every( food => 
food.source === 'plant');

const dinner = [{name: 'hamburger', source: 'meat'}, {name: 'cheese', source: 'dairy'}, {name: 'ketchup', source:'plant'}, {name: 'bun', source: 'plant'}, {name: 'dessert twinkies', source:'unknown'}];

console.log(isTheDinnerVegan(dinner))
// false

 

 

11. sortSpeciesByTeeth() === .sort()

const speciesArray = [ {speciesName:'shark', numTeeth:50}, {speciesName:'dog', numTeeth:42}, {speciesName:'alligator', numTeeth:80}, {speciesName:'human', numTeeth:32}];

const sortSpeciesByTeeth = arr =>
arr.sort((speciesObj1,speciesObj2) => speciesObj1.numTeeth > speciesObj2.numTeeth)

console.log(sortSpeciesByTeeth(speciesArray))

/*[ { speciesName: 'human', numTeeth: 32 },
  { speciesName: 'dog', numTeeth: 42 },
  { speciesName: 'shark', numTeeth: 50 },
  { speciesName: 'alligator', numTeeth: 80 } ]
*/

 

12. findMyKeys() ===findIndex()

const findMyKeys = arr => 
arr.findIndex( word => word === 'keys');


const randomStuff = ['credit card', 'screwdriver', 'receipt', 'gum', 'keys', 'used gum', 'plastic spoon'];

console.log(findMyKeys(randomStuff))

// 4

 

13. dogFactory()

const dogFactory = (name, breed, weight) =>  {
  return { _name: name, 
           _breed: breed, 
           _weight: weight,
          get name () {
            return this._name;
          },
          get breed () {
            return this._breed;
          }, 
          get weight () {
            return this._weight;
          },
          set name(newName) {
            this._name = newName;
          },
          set breed(newBreed) {
            this._breed = newBreed;
          },
          set weight(newWeight) {
            this._weight = newWeight;
          },
          bark() {
            return `ruff! ruff!`
          },
          eatTooManyTreats() {
            this._weight++
          }

}}