I did it, on June 1, 2020 I wrote this tweet:
B.@sincerelybritttToday I have decided to commit to #100DaysOfCode. I started it before and did not finish it. So today, June 1, 2020, I have decided to do it so that way I can look back 100 days from now and see how far I came.14:48 PM - 01 Jun 2020
and today I completed the #100DaysofCode Challenge and I am proud of myself! I stuck to it and I can finally say I am a part of the #100DaysofCode community. I will create a post in the near future with my advice and experience with #100DaysofCode, but for now I am going to just enjoy feeling accomplished.
Today, I focused on JavaScript Array of Objects Tutorial and completed a few labs. One consisted of finding the first win in a game and then returning the year. I used google and freeCodeCamp to accomplish it.
function nbaChampionshipWin(arr){
// console.log(arr)
let win = arr.find(year => year.result === "win");
// console.log(win)
if (win){
console.log(win)
return win.year
}else{
console.log(undefined)
return undefined
}
}
const record = [
{year: "2019", result: "lose"},
{year: "2018", result: "win"},
{year: "2017", result: "N/A"}
]
nbaChampionshipWin(record);
The nbaChampionshipWin
function in the code above takes in an array as an argument. The array is an array of objects. The challenge was to find and only return the first year that a team won. In order to do that I used the .find
method to return the value of the first element in the provided in the arr
that resulted in a "win".
To make sure that the function does not return any errors, it is best to use the toLowerCase()
method on the year.result
so that it will always be lowercased and wont return false if the result is capitalized.
function nbaChampionshipWin(arr){
let win = arr.find(year =>
year.result.toLowerCase() === "win");
if (win){
return win.year
}else{
return undefined
}
}
const record = [
{year: "2018", result: "lose"},
{year: "2017", result: "win"},
{year: "2016", result: "N/A"}
]
nbaChampionshipWin(record);
Please remember that when using .find
only the FIRST instance is returned. If you want to get multiple instances it is best to use the .filter
method. More information on .filter
here.
As always, thanks for reading! I will continue blogging, just not for 100 days in a row. Look out for my blog on my #100DaysofCode experience.
Sincerely,
Brittany
Song of the day:
Top comments (1)
Thank you KYLE!