Home > Software design >  i don´t know how acces to the pet object (EXCERCISE WITH OBJECTS IN JS)
i don´t know how acces to the pet object (EXCERCISE WITH OBJECTS IN JS)

Time:12-03

im doing a project-excercise of debugging using objects, im already done all the other´s excercises but im very stuck in this, i dont know how to solve.

    /*

Fix the `feedPet` function. `feedPet` should take in a pet name and return a
function that, when invoked with a food, will return the pet's name and a list
of foods that you have fed that pet.

*/

function feedPet(name) {
  const foods = [];
  return (food) => {
    return "Fed "   name   " "   foods.push(food)   ".";
  }
}

const feedHydra = feedPet('Hydra');

console.log(feedHydra('bones')); // Fed Hyrda bones.
console.log(feedHydra('Hercules')); // Fed Hyrda bones, Hercules.

const feedHippogriff = feedPet('Hippogriff');

console.log(feedHippogriff('worms')); // Fed Hyrda worms.
console.log(feedHippogriff('crickets')); // Fed Hyrda worms, crickets.
console.log(feedHippogriff('chicken')); // Fed Hyrda worms, crickets, chicken.

CodePudding user response:

push returns a new length of the array, but you want to have the items

function feedPet(name) {
  const foods = [];
  return (food) => {
    foods.push(food)
    return "Fed "   name   " "   foods   ".";
  }
}

function feedPet(name) {
  const foods = [];
  return (food) => {
    foods.push(food)
    return "Fed "   name   " "   foods.join(', ')   ".";
  }
}

const feedHydra = feedPet('Hydra');

console.log(feedHydra('bones')); // Fed Hyrda bones.
console.log(feedHydra('Hercules')); // Fed Hyrda bones, Hercules.

const feedHippogriff = feedPet('Hippogriff');

console.log(feedHippogriff('worms')); // Fed Hyrda worms.
console.log(feedHippogriff('crickets')); // Fed Hyrda worms, crickets.
console.log(feedHippogriff('chicken')); // Fed Hyrda worms, crickets, chicken.

CodePudding user response:

foods.push(food) returns the array length, which is the reason for the numbers.

Fixed your code to print the array correctly:

    /*

Fix the `feedPet` function. `feedPet` should take in a pet name and return a
function that, when invoked with a food, will return the pet's name and a list
of foods that you have fed that pet.

*/

function feedPet(name) {
  const foods = [];
  return (food) => {
    foods.push(food);
    
    // Equivalent to foods.join(", ");
    /*
    var ret = "";
    for(var i = 0; i < foods.length;   i)
    {
      ret  = foods[i];
      if(i < foods.length - 1)
      {
        ret  = ", ";
      }
    }
    */

    return "Fed "   name   " "   foods.join(", ")   ".";
  }
}

const feedHydra = feedPet('Hydra');

console.log(feedHydra('bones')); // Fed Hyrda bones.
console.log(feedHydra('Hercules')); // Fed Hyrda bones, Hercules.

const feedHippogriff = feedPet('Hippogriff');

console.log(feedHippogriff('worms')); // Fed Hyrda worms.
console.log(feedHippogriff('crickets')); // Fed Hyrda worms, crickets.
console.log(feedHippogriff('chicken')); // Fed Hyrda worms, crickets, chicken.

  • Related