Javascript 101 exercises help #94

I’ve seen a previous post from [Sep 2019] (101 exercises JavaScript need help with #94 - #3 by ShelbyHughes) but I’m still not picking up on it. I’m not sure what I’m doing wrong or if I’m missing something

Array is as follows:

> const books = [
>     {
>         "title": "Genetic Algorithms and Machine Learning for Programmers",
>         "price": 36.99,
>         "author": "Frances Buontempo"
>     },
>     {
>         "title": "The Visual Display of Quantitative Information",
>         "price": 38.00,
>         "author": "Edward Tufte"
>     },
>     {
>         "title": "Practical Object-Oriented Design",
>         "author": "Sandi Metz",
>         "price": 30.47
>     },
>     {
>         "title": "Weapons of Math Destruction",
>         "author": "Cathy O'Neil",
>         "price": 17.44
>     }
> ]

[almond-buttercup-peripheral] (Glitch :・゚✧)

so far, I have

> function highestPriceBook(books) {
>   var sum= 0;
>   for (var j = 0; j = books.length; j++)
> return books.sort((a, b) => b.price - a.price);
> }
> 
> highestPriceBook(books)[0];

The answer should show

assert(highestPriceBook(books)),
            {
                title: "The Visual Display of Quantitative Information",
                price: 38.0,
                author: "Edward Tufte"
            },
            "Exercise 94"

Thank you anyone who is reading this.

Hi @Alexander_Castaneda and welcome to the community! :wave:
Try something like this:

function mostExpensive(bookArray) {
return bookArray.sort((a, b) => b.price - a.price);
}

mostExpensive(books)[0];

Is it working now?
Thanks and happy glitching!
Tiago Rangel

assert(highestPriceBook(books)),
            { ...

I think you have an extra ) on that first line, the expected value and exercise label below should be additional arguments to the assert.

I attempted this method with no avail. I have the glitch handle in the post if that’ll help.

I used:

function highestPriceBook(books) {
return books.sort((a, b) => b.price - a.price);
}

highestPriceBook(books)[0];

I get that it’s irresistible to copy and paste code from someone who says they’ve solved it for you. However, it’s not integrated into your exercise testing framework.

Maybe you could take this opportunity to read through the solution and see what each part means, and then think about how it should fit into the exercise file.

Also important in this particular case, it’s a good opportunity for you to figure out what really changed in the other poster’s snippet and think about why he considers that to solve the problem. It’s kind of subtle.

So go ahead and start off with this: line-by-line, what does the posted code do?

3 Likes

Having learnt that each step in coding is dependent on the previous one, I found that my difficulties grew rapidly as I attempted to progress through the course without a thorough grasp of a topic I still found unclear.

When you get stuck on a piece of homework, I recommend practicing by writing pseudo code for the question and posting it online for others to help you out. I believe you will receive more thorough assistance that will shed light on the issue you are having and reduce the likelihood that others will assume you are seeking a simple answer.

This is a pdf on writing pseudo-code. I’ve found a lot of the times, I would see the solution when I stepped back and just wrote out the code thinking about the concept.

It’s worth noting that I’m still very much a novice in the world of coding, so there may be more polished alternatives.

1 Like