Challenge 18 Megathread

I love how that thread is over 10 years old, and shows how the code evolved over the decade

Thanks Dithered! this helped me too! :smile:

This challenge really made me think :slight_smile: Here is my solution.

Spoiler 1. I created two objects with two properties each, one for the menu choice and one for the number of votes. 2. I then iterated through the given list of choices array. 3. On the first iteration I used the filter method to count the number of that particular (first) element (i.e. menu choice) in the array and I assigned the value to the number of votes property in the first object. I also assigned the menu choice to that same object. 4. Then I created an if case and once the list of choices [i] was not equal to list of choices [i+1] (i.e. thus we found a different choice) I applied the filter method and counted the number of votes for the second choice. I also assigned the menu choice to the second object. 5. That way I got two objects with the number of votes and the name of choice. 6.Then I created an if case to compare the number of votes in the two objects and returned the name of menu choice with the biggest number.
This is probably the most convoluted code I have written in this coding challenge event, 11 lines not counting declaring the objects.

How many lines is your code?

1 Like

An improvement on this approach is to also keep track of the current max as you loop through so then at the end of the loop you already have your answer.

let maxChoice = {
    total: 0,
    choice: null
  };

then at bottom of loop:

if (choiceMap[choice] > maxChoice.total) {
      maxChoice.total = choiceMap[choice];
      maxChoice.choice = choice;
    }

So then you just need to return maxChoice.choice to get the answer.

3 Likes

It will save time. But if we have a tie?

It depends… I could put all my 100 commands in one line kkkk. :smiley:

Ex. It could be written in 5 lines the same

return (currentLargestNumber > previousLargestNumber) ? currentLargestNumber : previousLargestNumber;

I think the number of the lines it’s not a good comparisson parameter.

1 Like

Your code should be more versatile about the menu. And if the preferences change?

const listOfChoices = [
  "Chicken Dinner",
  "Chicken Dinner",
  "Chicken Dinner",
  "Ice Cream Sandwich", 
  "Ice Cream Sandwich",
  "Tim Biebs",
  "Tim Biebs",
  "Tim Biebs",
  "Tim Biebs",
  "Tim Biebs",
  "Tim Biebs",
  "Tim Biebs",
  "Nanaimo Bar",
  "Nanaimo Bar",
  "Nanaimo Bar",
  "Nanaimo Bar",
  "Nanaimo Bar",
  "Nanaimo Bar"
];
1 Like

I did it almost identical to that but you can keep track of the current max occurrence string in the object. Then you don’t need to sort it again at the end.

![Screen Shot 2021-12-09 at 2.05.36 PM|690x296]
This image violated the blog rules. Removed by moderators. Please do not share complete correct answer.

3 Likes

I only recall there being two tests when I ran it just after midnight, now there’s a third test with a longer array. Still only two options in the array though.

But it goes to show they could easily add an input item like yours above, and it will test how versatile the solutions people are running are.

1 Like
  if (objData.votes[test] > maxVotes) {
    maxVotes=objData.votes[test];
  }

Thanks, I moved this up into my initial loop, it saved me only two lines of code, but looks considerably cleaner now, and saves a whole for loop structure, which especially on a larger menu, should save clock cycles.

Hmm, now my brain’s telling me there’s probably a way to use .map() to optimize further.

Of course, no change has been made to my tie-breaker situation, first one indexed wins. So early bird gets the worm. FIFO seems like a fair tie breaker.

You’ll run into this as “case” and “switch” in other languages.

Yes, I’ve seen the “case” and “switch” in C++ but I haven’t used it nearly as often yet as I only started learning C++ 9 months ago.

Well, I should say I tried to learn C++ back when I was 17, but college had way way too many distractions and I flunked out.

1 Like

Yeah, I just trying to highlight that it’s not practical to write inside the function ephemeral references.

listOfChoices[I] == “Chicken Dinner”

1 Like

This worked for me too. Thanks Dithered!

i used Object.entries() to convert the object in to a list of lists, which returns something like this: [[key, value], [key, value]]. and then used .sort() based on the elements 1st index which would be the value (aka. count).

1 Like

mine works with this as well,
image

This is the list that is returned, I just return the first element and then the name from that element.

2 Likes

I had considered the 2d array approach.
Nicely done.

Seeing all this, I wish I’d thought about using .sort() lol

Superb!! :clap: I had the same idea using an ordered list. :laughing:

I kinda wish I had just used a simple integer counter rather than 2 arrays and then checking the length of each.

Not sure what i need to do to check through the other items tried this and i am stuck