Challenge 17 Megathread

Welcome to challenge 17! Use this thread for any and all questions relating to challenge 17.

1 Like
const judgeVegetable = (vegetables, metric) => {
  return vegetables.reduce((acc, cv) => cv[metric] > acc[metric] ? cv : acc).submitter;
}

Here is my answer!

1 Like

Just sort!

judgeVegetable=(v,m)=>v.sort((a,b)=>b[m]-a[m])[0].submitter
3 Likes

Literally identical, except for the parameter names. I don’t think ours is very readable unless spread to more lines though.

const judgeVegetable = (vegetables, metric) => {
  return (vegetables.reduce((Leader, Submission) => Submission[metric] > Leader[metric] ? Submission : Leader)).submitter
}

to

const judgeVegetable = (vegetables, metric) => {
  firstplace = vegetables.reduce ( (Leader, Submission) =>{
    return Submission[metric] > Leader[metric] ? Submission : Leader
    })
  return firstplace.submitter
}

I really like @injust 's 1 line sort() solution above.

What the fuck is going on again with my code ? I passed the “Test Code” and again I got “Submit Code” failure…

I got the same problem

They don’t use redness or plumpness as the metric in the submission input, pretty misleading i know.
Allow your function to be able to compare any metric input and you’ll be able to submit and pass all the tests.

1 Like

I have the same problem too

My code is passing the tests, but does not pass when I click submit. I checked in the console and my code is giving the correct answer so I’m not sure what’s going on.

judgeVegetable = (vegetables, metric) => {
  let bestVeggie = vegetables[0];  
  for (let i = 0; i < vegetables.length; i++) {
    if (metric === "redness"){
      if (vegetables[i].redness > bestVeggie.redness) {
        bestVeggie = vegetables[i];
      }
    } else { 
      if (vegetables[i].plumpness > bestVeggie.plumpness) {
        bestVeggie = vegetables[i];
    }
  }
}
  return bestVeggie.submitter
}

I got the same problem!

I cannot submit my code, but it’s already considering a generic metric.

const judgeVegetable = (vegetables, metric) => {
  // Code here!
  console.log(metric);
  var i = 0;
  var j = 1;
  var winningVeggie = vegetables[i][metric];
  while (vegetables.length < j) {
    currentVeggie = vegetables[j]
    console.log(currentVeggie)
    if (winningVeggie[metric] > currentVeggie[metric]) {
      console.log(winningVeggie['submitter'] + " beats " + currentVeggie['submitter'] + " when comparing " + metric);
      console.log("Current winner: " + vegetables[i]['submitter']);
      j++
    } else {
      console.log(currentVeggie['submitter'] + " beats " + winningVeggie['submitter'] + " when comparing " + metric);
      console.log("Current winner: " + vegetables[i]['submitter']);
      i = j;
      j++;
    }
  }
  return vegetables[i]['submitter'];
};

Same here. My code passes the tests but won’t submit.

1 Like

Got it to work by adding a check on the variable holding index of highest rated vegetable. I did not check this initially and that variable ends up being null. Passed the Test Code but couldn’t submit. Was able to submit once I figured out I needed to check condition on that variable after my loop.

Thanks man. By following your guidance I was able to submit the code ultimately.

1 Like

similar to others:

const judgeVegetable = (vegetables, metric) =>
  vegetables.reduce((pickedVegetable, vegetable) =>
    pickedVegetable[metric] > vegetable[metric] ? pickedVegetable : vegetable
  ).submitter;

Keep in mind that sort is overkill when trying to find a max value in an array (intuitively, it takes more time to sort all elements of an array than to just find the max)

More precisely, sorting takes O (n log n) at best, finding max takes O(n) time

3 Likes

To sort an array of objects by a variable property name, whose value is numeric, I used a destructuring assignment plus the computed property value for ‘metric’. Because I overcomplicate things. :joy:
(I wanted to practice those 2 features of the language).

Summary
const judgeVegetable = (vegetables, metric) => {
  const sortedVeg = vegetables.sort(({[metric]:a}, {[metric]:b}) => b - a);
  return sortedVeg[0].submitter;
}
1 Like

1 Like

I like using for of loops but I keep forgetting that it’s ‘for of’ and not ‘for in.’

const judgeVegetable = (vegetables, metric) => {
  // Code here!
  var value = 0;
  var name = "";
  for(var veggie of vegetables){
    if (veggie[metric]>value){
      name = veggie['submitter'];
      value = veggie[metric];
    }
  }
  return name;
}
4 Likes
const judgeVegetable = (vegetables, metric) =>
  vegetables.reduce((winner, contestant) =>
    contestant[metric] > winner[metric] ? contestant : winner
  ).submitter;
1 Like