0

I have an exercise that requires me to use a for loop to iterate through the object 'tutorPetTypes' and calculate the collective total number of cats owned by the tutors.

Here's my current code. It runs but results in totalCats = 0, whereas It should be 7.

If I console.log(tutor) as part of the if statement, I can see that it's correctly iterating through the tutors but I'm obviously missing something in terms of correctly checking the array for "cat".

const tutorPetTypes = {
  'Sarah': ['cat'],
  'Jim': ['dog', 'dog'],
  'Joe': ['mouse'],
  'Róisín': ['cat','cat','cat','cat','cat','dog'],
  'Edd': ['lizard', 'cat'],
  'Lewis': ['bearded dragon', 'tortoise']
}

let totalCats = 0

//Solution to be provided below

for (const tutor in tutorPetTypes) {
  for (let i = 0; i < tutor.length; i++) {
      if (tutor[i] === "cat") {
      totalCats++;
    };
  };
};

Any help would be greatly appreciated.

6
  • Does this answer your question? Javascript: compare variable against array of values Commented Oct 27, 2023 at 13:40
  • 1
    Have a look at docs) what for .. in really does. You probably want to use Object.values You also could set a breakpoint somewhere in the loop and check for instance the value of tutor so you would see, it's not what you expect it is ... Commented Oct 27, 2023 at 13:41
  • 2
    Most importantly, learn to use the debugger to step through your code so that you can see what value tutor has, what tutor[i] has, and why comparing it to "cat" makes no sense. Commented Oct 27, 2023 at 13:43
  • Does this answer your question? How do I loop through or enumerate a JavaScript object? Commented Oct 27, 2023 at 13:48
  • @PM77-1 Probably not, because the problem seems not to be searching the array, but getting the arrays from the object ... Commented Oct 27, 2023 at 13:49

2 Answers 2

1

You should consider using the for...of loop on the values of the object instead. Example below.

I also included an alternative in case you are allowed to get away from the for loop. In that alternative, you could map the object's values and filter the animals array of each person to return only the cats. Then, flatten the array, since the map result will be a nested array of arrays, and get it's length.

const tutorPetTypes = {
  Sarah: ['cat'],
  Jim: ['dog', 'dog'],
  Joe: ['mouse'],
  Róisín: ['cat', 'cat', 'cat', 'cat', 'cat', 'dog'],
  Edd: ['lizard', 'cat'],
  Lewis: ['bearded dragon', 'tortoise'],
}

let totalCats = 0

for (const animals of Object.values(tutorPetTypes)) {
  for (let i = 0; i < animals.length; i++) {
    if (animals[i] === 'cat') {
      totalCats++
    }
  }
}

console.log(totalCats)

// To be consistent with for...of, you can do:
totalCats = 0
for (const animals of Object.values(tutorPetTypes)) {
  for (const type of animals) {
    if (type === 'cat') {
      totalCats++
    }
  }
}

console.log(totalCats)

// Another alternative would be:
totalCats = Object.values(tutorPetTypes)
  .map((animals) => animals.filter((type) => type === 'cat'))
  .flat().length

console.log(totalCats)

Sign up to request clarification or add additional context in comments.

Comments

0

Thank you everyone for your responses.

I find it interesting how no one was willing to provide a solution using for... in, and I don't mean that in a critical way, just observational. As clearly there are more advanced functions that provide a more suitable solution to this exercise given that so many people suggested it i.e. Object.values. However, the exercise specifically required that I use the a for... in loop. (I don't know why) And your responses did help me in analysing my solution and eventually coming up with a working one. Which is this:

for (const tutor in tutorPetTypes) {
  for (let i = 0; i < tutor.length; i++) {
      if (tutorPetTypes[tutor][i] === "cat") {
      totalCats++;
    };
  };
};

It simply needed [tutor][i] changing to tutorPetTypes[tutor][i]

Previously it was iterating through each letter of the tutors name, hence === 'cat' returned 0.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.