ES6 Practice - Let, Const, Fat Arrow Functions, Default Arguments, Template Literals

Let and Var



Use let and const and try to use these new features

Replace all the vars with let and const

var name = 'John'  
var age = 101  
var pets = ["cat", "dog"]

var petObjects = []

for (var i = 0; i < pets.length; i++){  
  var pet = {type: pets[i]}
  if (pets[i] === "cat"){
    var name = "fluffy"
  } else {
    var name = "spot"
  }
  pet.name = name
  petObjects.push(pet)
}

ES6 Functions


Fat arrow functions.

re-write this .map() using a fat arrow function:

const vegetables = carrots.map(function(carrot){  
    return {type: "carrot", name: carrot}
})

re-write this .filter() using a fat arrow function:

friends = people.filter(function(person){  
    return !!person.friendly
})

re-write the following functions to be fat arrow functions:

function doMathSum(a, b){  
    return a + b
}

var produceProduct = function(a, b){  
    return a * b
}

write a functions that takes a firstName, a lastName, an age and returns a string like the following:
Hi Kat Stark, how does it feel to be 40?

firstName should default to "Jane"
lastName should default to "Doe"
age should default to 100


Write a function that uses the shorthand property to make an object (.map)
Have the following code use the shorthand syntax to take up one line.

const dogs = animals.filter((animal)=>{  
  if (animal.type === "dog"){
    return true
  } else {
    return false
  }
})

Template Literals



Write a function that will take location and name and output this if location="Hawaii" and name="Janice":

Hi Janice!

Welcome to Hawaii. 

I hope you enjoy your say. Please ask the president of Hawaii if you need anything.  

If you didn't do that with Template Literals, do it now with Template Literals.