Open eadehemingway opened 5 years ago
answers for the above:
const colorsArray = ["yellow", "red", "turquoise", "green", "blue"]
1.
const filteredArr = colorsArray.filter((str)=>{
return str.length > 5
} )
2a.
const capArr = [];colorsArray.forEach((str)=>{
const upperCase = str.toUpperCase()
capArr.push(upperCase)
})
2b.
const capArrMap = colorsArray.map((str)=> {
return str.toUpperCase()
})
const longest= colorsArray.reduce((acc, str)=>{
if(acc.length> str.length){
return acc
}else{
return str
}
})
const myDog = {
name: "Clyde",
breed: "boxer",
age: 3,
}
const anotherDog = {
name: "rex",
breed: "pug",
age: 9
}
myDog.gender="female"
anotherDog["gender"] = "male"
myDog.age = 7
myDog.playFetch = ()=> {
console.log('i am playing fetch')
}
myDog.myName = function(){
console.log('my name is ', this.name)
}
delete anotherDog.breed
function hasBreed(obj){
return 'breed' in obj
}
const keys = Object.keys(myDog)
const length = keys.length
const copy = {...myDog}
copy.name = "copy"
const anotherCopy = Object.assign({}, myDog)
anotherCopy.name= "anotherCopy"
arrays
use the filter method to create an array with all colors from colorsArray that are longer than 5 letters long. expected answer is [ 'yellow', 'turquoise']
a) use the forEach method to create an array that has each color from colorsArray in capital letters. expected answer is [ 'YELLOW', 'RED', 'TURQUOISE', 'GREEN', 'BLUE' ]
b) create the same array using the map method
use the reduce method to find the color with the most amount of letters in colorsArray. expected answer is 'turquoise' (as a string, not in an array)
objects
create an object called anotherDog with the properties "name", "breed" and "age" (you can choose the values for these properties)
without touching the variable add the information gender:female to myDog
without touching the variable add the information gender:male to anotherDog using a different method to the one you used for question 2.
change the age of myDog from 3 to 7
without touching the variable create a method on the myDog object called 'playFetch' that, when called console.logs 'i am playing fetch'
create a method on myDog called "myName" that uses the 'this' keyword so that when called it console.logs 'my name is Clyde' .
delete the property 'breed' from anotherDog
create a function called hasBreed that takes an object and checks if it has the property 'breed'. hasBreed(myDog) should return true, hasBreed(anotherDog) should return false.
calculate how many key value pairs myDog contains
create a copy of myDog object so that if you change the name of the copy it does not change the name of the original.