Challenge 18 Megathread

I was just trying to do some testing to better understand new Set.
Using the JS sandbox on mozzila I’m having some trouble with the specific syntax he showed, but well. looks at my handle I’m used to that.

Thank you for the tip! Modified the code and it works!

I did mine the hard way. Since there were only 2 options and an odd number of astronauts, I nested an if inside a foreach, and compared the entry in the object to the object[0] position, then used counters to do the rest.


This is neat.
I’ve done some fiddling now, and read through the prospectus.
It will work with the datasets provided, but when this says “Majority Element” it literally means majority. As a method to meet some kind of required quorum, which is pretty neat.

In our use scenario, it would fall flat if we introduce a more options. If we added Tacos, and Sushi to the list for instance. We would have 5 voters, and 4 outcomes. Assuming everyone casts a vote, there’s the potential for the winning item to have only 2 votes.
[Sushi, Chicken, Sushi, Taco, Icecream]

Since the winner isn’t a majority (more than 50%), it will be zeroed out in the count before the final element, and thus the final element, with only a single vote would win by algorithm. [Which can be checked for with the N/2 validator]

Which is great for any system where a vote is considered void if no recipient garners a majority support.

1 Like

I found this challenge to be hard.

I know how I would have done it in another language, and I couldn’t make it work in JS. I either don’t know what it’s called or if it even exists.

No matter, I got it working and learnt something in the process.


Passing one test but getting the wrong menu item returned. What am I missing?

What technique/method would you have liked to use that you couldn’t find?

These are the things I was hoping to see more of in the discussion :slight_smile: It gives me a reason to go fiddle with alternative solutions.

1 Like

listOfChoices[0].length is the same as, for example: “chicken sandwich”.length . It will give you the length of the string in the first position of the list.

listOfChoices is an array of strings.
So when you ask for the length of an element of that array you are giving yourself a character count for the word (string) found at that array index.

So essentially what you have imaged here is.
If the words “Chicken Dinner” are shorter than the words “Ice Cream Sandwich”, return “Ice Cream Sandwich”

You’ll need to provide some kind of logic to determine which word exists within the array more frequently.

[ See link here for a visible representation of what you’re testing ]
JS Fiddle - “string”.length

1 Like

Love hearing you had fun! :smiley:

1 Like

Yea, I never saw that before either. I thought it was pretty clever

I tried doing this but one test is failing. My string is getting returned correctly.

That’s because you are checking against a predetermined lunch choice of “Chicken Dinner” and “Ice Cream Sandwich”. While it works for the first test case, it won’t necessarily work for any other test case that has lunch choices other than “Chicken Dinner” and “Ice Cream Sandwich”.

I’m a bit stuck here, I think my problem is line 6 based on what Colo_1 answered in a different reply (counting the length of the string instead of the number of elements in the array), but I’m not sure how else to do it. I tried working out a way to do it with listOfChoices.forEach but I’m not doing it correctly either:

const chooseLunchWinner = (listOfChoices) => {
  //Code here!
  let modeMap = {};
  let maxOfX = listOfChoices[0], maxCount = 1;
  for (let i = 0; i < listOfChoices.length; i++)
    let x = listOfChoices[i];
    if(modeMap[x] === null)
        modeMap[x] = 1;
    } else {
    if(modeMap[x] > maxCount)
        maxOfX = x;
        maxCount = modeMap[x];
  return maxOfX;

Thank you!

Don’t check with a specific value of the choice

Declare a variable with first item, then do loop starting from second item and check with the value that yo declared.

He used var foo = [ Set(bar)]
This is because new Set() on its own would create an object, but he needed the values in the set to be in an array instead so that he could access them by index.
This might help you figure it out:


I’ve mentioned before that I develop programs for AutoCAD. I use AutoLISP for 99% of it so far but I’m learning C++ now to do more.

In AutoLISP there is a function called cond. What it does is allows a user a large theoretical number of conditions to evaluate instead of dozens of if statements.

This example shows just 6 cases out of a total of 94 possible cases for figuring out a plot size:

((and (= dx 215.9) (= dy 279.4) (or (= count nil) (= count 0.0) (= count 1.0))) (setq plotsize))
((and (= dx 279.4) (= dy 215.9) (or (= count nil) (= count 0.0) (= count 1.0))) (setq plotsize))
((and (= dx 215.9) (= dy 355.6) (or (= count nil) (= count 0.0) (= count 1.0))) (setq plotsize))
((and (= dx 355.6) (= dy 215.9) (or (= count nil) (= count 0.0) (= count 1.0))) (setq plotsize))
((and (= dx 279.4) (= dy 431.8) (or (= count nil) (= count 0.0) (= count 1.0))) (setq plotsize))
((and (= dx 431.8) (= dy 279.4) (or (= count nil) (= count 0.0) (= count 1.0))) (setq plotsize))
);end cond

This was my thinking of this challenge. I thought I had three cases to consider: Empty Array, Array with value1, and Array with value2.

So if I had anything more than two cases to consider, I’d be using a cond function in my day job.

However, I figured out how to do todays challenge with just two conditions.

1 Like

It seems good from first glance, however I think you are getting caught up at the if (modeMap[x] === null) check. Try printing out your modeMap at the end of the function and you will see. This is because the === is a strict type check, and therefore goes into this block of code if and only if the modeMap[x] is null, whereas it could be undefined or something.

I suggest you change that conditional check to if (!modeMap[x]), will still give you what you’re looking for. Logically the code makes sense.

Also at the end when you were trying to console.log the function, you forgot to pass it a listOfChoices argument.

1 Like

Ahh thank you so much! changeing the conditional check worked.

1 Like

Did some Googling and found this. A few tweaks and I got it. Yahoo.