CodeNewbie Community 🌱

Cover image for Commit20 //#JavaScript30 - Day 7 [Array Cardio Day 2]
Janet Webster
Janet Webster

Posted on

Commit20 //#JavaScript30 - Day 7 [Array Cardio Day 2]

Photo of Array Cardio console log


I am learning with Skillcrush. Please check them out and consider signing up if you're looking to break into tech! Receive $250 off your course by using the link above. Use the coupon code: TWIXMIXY
NOTE: this discount is for Break Into Tech + Get Hired package exclusively. It's the same program I am participating in!


Today's challenge comes from JavaScript30.

My migraine continued into today. I worked out with my trainer this morning and took some vitamin B and that seemed to really help dissipate it. My jaw is still sore from clenching all night long.

I'm getting to this a bit later in the day than what I wanted to. I received my resume back from kbresumes.com, so I needed to tweak that some and then disperse it out. Then I had a call about a contract position with a recruiter. I'm not super actively looking for work right now, but if something does come my way I will explore it.

This position would be for Project Management, which is likely what my next role will be even though I'm beginning to position myself to head into Product Management. A friend on LinkedIn sent me some books to read about getting into Product, so I have a couple audible credits I can use on those.

OK! Let's get into it. Lots to do today with Survivor being on tonight and I have a training meeting with Charlotte Roller Derby.

JavaScript30 - Day 7

Starting off with some predefined arrays for us!

    // ## Array Cardio Day 2

    const people = [
      { name: 'Wes', year: 1988 },
      { name: 'Kait', year: 1986 },
      { name: 'Irv', year: 1970 },
      { name: 'Lux', year: 2015 }
    ];

    const comments = [
      { text: 'Love this!', id: 523423 },
      { text: 'Super good', id: 823423 },
      { text: 'You are the best', id: 2039842 },
      { text: 'Ramen is my fav food ever', id: 123523 },
      { text: 'Nice Nice Nice!', id: 542328 }
    ];
Enter fullscreen mode Exit fullscreen mode

// Array.prototype.some() // is at least one person 19 or older?
.some will check if at least one thing in your array meets what you are looking for.

    const isAdult = people.some(function(person) {
      const currentYear = (new Date()).getFullYear();
      if(currentYear - person.year >= 19) {
        return true;
      }
    });
    console.log(isAdult);
Enter fullscreen mode Exit fullscreen mode

So we are running this function against the people array. Creating a new function to get the currentYear and then subtracting the year born of the person. If that is equal to or greater than 19, the console log returns true.

True some people are adults

Then we rewrote the function as an arrow function.

    const isAdult = people.some(person => {
      const currentYear = (new Date()).getFullYear();
      return currentYear - person.year >= 19;
    });
    console.log(isAdult);
Enter fullscreen mode Exit fullscreen mode

Same result, true.

Rewritten again to be an implicit return.

const isAdult = people.some(person => ((new Date()).getFullYear()) - person.year >= 19 );

    console.log(isAdult);
Enter fullscreen mode Exit fullscreen mode

TRUE!

// Array.prototype.every() // is everyone 19 or older?
.every "is everybody 19?" - so I'm guessing it's asking does each item from the array meet this criteria?

const allAdults = people.every(person => ((new Date()).getFullYear()) - person.year >= 19 );

    console.log(allAdults);
Enter fullscreen mode Exit fullscreen mode

FALSE. So when we are logging this out it says not all the people are 19.

// Array.prototype.find()
.find is kind of like filter, instead of a subset, it returns the first item it finds.

    const comment = comments.find(function(comment) {
      if(comment.id === 823423) {
        return true;
      }
    });
    console.log(comment);
Enter fullscreen mode Exit fullscreen mode

When we run this console log it pulls back the following specific comment.

{text: 'Super good', id: 823423}
id: 823423
text: "Super good"
[[Prototype]]: Object
Enter fullscreen mode Exit fullscreen mode

The instructor then challenged us to pause the video and turn our function into an implicit return... and I think I got it!!

const comment = comments.find(comment => comment.id === 823423);
    console.log(comment);
Enter fullscreen mode Exit fullscreen mode

happy cry

Reading the same output in my console log!

// Array.prototype.findIndex()
.findIndex find something within the array

const index = comments.findIndex(comment => comment.id === 823423);
    console.log(index);
Enter fullscreen mode Exit fullscreen mode

The console log put out 1, which means our ID matches the second item in the array.

Now they want us to delete the item from the array.

.splice is one way to do this.

const index = comments.findIndex(comment => comment.id === 823423);
    console.log(index);

comments.splice(index, 1);

    console.table(comments);
Enter fullscreen mode Exit fullscreen mode

Splice to delete

And that's it! Just working on a bunch of different array methods today. It was fun to try out some new things.

I know this all will have to be something I practice over and over again. Ideally I will get a few projects that mean something to me soon, so that I can really practice these methods.

Check out all the code here!

Top comments (5)

Collapse
 
lorenzoluis profile image
Lorenzo Luis

Congratulations on completing Day 7 of the JavaScript30 challenge! Array Cardio Day 2 focused on further enhancing your array manipulation skills in JavaScript. Throughout the challenge, you likely worked with methods like some(), every(), find(), and findIndex() to perform various checks and searches within arrays. By practicing these methods, you gained valuable experience in efficiently working with array data, validating conditions, lesturf, and retrieving specific elements based on certain criteria. These exercises provide a solid foundation for utilizing array methods effectively in your JavaScript programming and problem-solving endeavors. Keep up the great work!

Collapse
 
jerry3564 profile image
jerry3564 • Edited

In the context of the challenge, "Commit20" appears to be a commit message, possibly related to a version control system like Git, indicating that you made changes or progress on the project spiritual empath quotes. Each day of the JavaScript30 challenge involves working on a specific project or coding exercise, and developers often use version control to track their changes and commit their code as they make progress.

Collapse
 
ritafoster profile image
Rita Foster

That could be a great option for us and we can use it to find what we need to know about this topic. Also at this site pressurecleaningportmacquarie.com.au/ I saw people are using the best services with the best results.

Collapse
 
fzfda profile image
fzfda

Since I don't have access to real-time data or your specific course progress, I recommend checking the JavaScript30 website or your course materials for detailed instructions for garage door repair mckinney and code examples related to "Array Cardio Day 2." If you have specific questions or need assistance with certain aspects of the challenge, feel free to provide more details, and I'll be happy to help!

Collapse
 
melospizaquize profile image
rottenuncertain

Despite having a lot on your plate soccer random, you manage to consistently put in time for practise and education, and it never ceases to amaze me. I hope you have a chance to recharge with some downtime and gentle exercise.