Home > Software design >  Returning the highest 'cuteness' rating in cats object
Returning the highest 'cuteness' rating in cats object

Time:09-15

How can I fix this problem. Actually this is an exercise about how to use debugger but I couldn't solve the problem.

Fix the cutestCat function. Should return the cat with the highest cuteness rating.

*/

function cutestCat(cats) {

  let cutest;
  let i = 0;
  debugger
  while (i < cats.length) {
    const cat = cats[i];
    if (cat > cutest) {
      cutest = cat.cuteness;
    }
    i  ;
  }

  return cutest;
}

const cats = [
  { name: 'Fluffy', cuteness: 9 },
  { name: 'Princess', cuteness: 6 },
  { name: 'Tiger', cuteness: 7 },
  { name: 'Indie', cuteness: 5 },
]

console.log(cutestCat(cats)); // { name: 'Fluffy', cuteness: 9 }

CodePudding user response:

Fix with either...

function cutestCat(cats) {
  let cutest=-MAX_SAFE_INTEGER, cutestCat;
  let i = 0;
  debugger
  while (i < cats.length) {
    const cat = cats[i];
    if (cat.cuteness > cutest) {  // note the new comparison
      cutest = cat.cuteness;
      cutestCat = cat;            // note the recording of the object
    }
    i  ;
  }
  return cutestCat;  // note returning the object
}

Or, prettier...

function cutestCat(cats) {
  let sorted = cats.slice().sort((a, b) => b.cuteness-a.cuteness);
  return sorted.length ? sorted[0] : null;
}

CodePudding user response:

You have two issues:

  1. you need to compare cat cuteness not cats ie:
if (cat.cuteness > cutest.cuteness){
}
  1. this comparison requires that the cutest variable holds a cat with a cuteness property, but starts as undefined which will give you an error. One possible fix is starting with it as the first cat in the array and initilizing i as 0.

This is the whole snippet:

function cutestCat(cats) {

  let cutest = cats[0];
  let i = 1;
  while (i < cats.length) {
    const cat = cats[i];
    if (cat.cuteness > cutest.cuteness) {
      cutest = cat;
    }
    i  ;
  }

  return cutest;
}

const cats = [
  { name: 'Fluffy', cuteness: 9 },
  { name: 'Princess', cuteness: 6 },
  { name: 'Tiger', cuteness: 7 },
  { name: 'Indie', cuteness: 5 },
]

console.log(cutestCat(cats));

CodePudding user response:

The problem lies in the if of the while loop. You are comparing cat(object) variable with cutest(numeric) variable.

So rather that storing cuteness in cutest variable, store the object itself. And then you can compare cuteness of both those objects in the if condition.

That is:

function cutestCat(cats) {

  let cutest = cats[0];
  let i = 1;
  debugger
  while (i < cats.length) {
    const cat = cats[i];
    if (cat.cuteness > cutest.cuteness) {
      cutest = cat;
    }
    i  ;
  }

  return cutest;
}

const cats = [
  { name: 'Fluffy', cuteness: 9 },
  { name: 'Princess', cuteness: 6 },
  { name: 'Tiger', cuteness: 7 },
  { name: 'Indie', cuteness: 5 },
];

console.log(cutestCat(cats)); // { name: 'Fluffy', cuteness: 9 }

You might also want to write some conditions to check if the array passed (i.e. cats) is empty or not.

CodePudding user response:

There are 2 simple ways to solve this problem. Reduce

function cutestCat(array) {
  return array.reduce((acc, rec) => {
    console.log("acc", acc);
    return acc.cuteness < rec.cuteness ? rec : acc;
  });
}

For loop

function cutestCat(array) {
  if (!array.length) {
    return "some default value";
  }
  let result = array[0];
  for (let i = 1; i < array.length; i  = 1) {
    const current = array[i];
    if (current.cuteness > result.cuteness) {
      result = current;
    }
  }
  return result;
}

In your example, you need to make a comparison by the cuteness field, and not compare objects. All objects have different references, so your condition won't work.

CodePudding user response:

You can use array.forEach to loop through them and check for the highest cuteness, like so:

function cutestCat(cats) {
  let cutest = cats[0]; // to avoid null reference
  cats.forEach(cat => {
    if (cat.cuteness > cutest.cuteness) {
      cutest = cat;
    }
  });
  return cutest;
}

CodePudding user response:

function cutestCat(cats) {

  let cutest;
  let i = 0;
  while (i < cats.length) {
    const cat = cats[i];
    // if there is no cutest cat to start with, it means this is the
    // first cat, set it as the cutest cat and skip the cuteness
    // comparison
    if(!cutest) {
      cutest = cat;
      continue;
    }
    // if there is already a cat that is considered cutest,
    // compare it with current cat and update cutest if
    // the current cat is more cuter than the one that is already
    // considered cutest
    if (cat.cuteness > cutest.cuteness) {
      cutest = cat;
    }
    i  ;
  }

  return cutest;
}

const cats = [
  { name: 'Fluffy', cuteness: 9 },
  { name: 'Princess', cuteness: 6 },
  { name: 'Tiger', cuteness: 7 },
  { name: 'Indie', cuteness: 5 },
]

console.log(cutestCat(cats)); // { name: 'Fluffy', cuteness: 9 }

  • Related