Accessing objects to find most frequent date

What am I doing wrong? Please help me.

// Iterate over store2's sale dates data to find which day had the most total number of sales. Return the date with the most sales.
    'sale dates': {
        'Dark Chocolate Crunchies': ['2015-01-06', '2015-01-06', '2015-01-06', '2015-01-08'],
        'Mint Wafers': ['2015-01-06', '2015-01-07', '2015-01-07', '2015-01-09'],
        'Peppermint Poppers': ['2015-01-08', '2015-01-08', '2015-01-09', '2015-01-09', '2015-01-10', '2015-01-10', '2015-01-10'],
        'Peanut Butter Buttered Peanuts': ['2015-01-07', '2015-01-07'],
        'Berry Bites': ['2015-01-06', '2015-01-09', '2015-01-09', '2015-01-09', '2015-01-10', '2015-01-10'],
        'Caramel Twists': ['2015-01-06', '2015-01-06', '2015-01-07', '2015-01-08', '2015-01-08', '2015-01-09', '2015-01-09', '2015-01-10', '2015-01-10', '2015-01-10'],
        'Banana Bunches': ['2015-01-10', '2015-01-10']
    }
function loopingData2() {
  let most = 0
  let result
  let datesWithCounts = {}
  //access sale dates object
  let datesByCandyName = store2[`sale dates`]
  //get array values
  for (let candy in datesByCandyName){
    for (let i = 0; i < datesByCandyName[candy].length; i++){
        if(!datesWithCounts[datesByCandyName][candy][i]){
          datesWithCounts[datesByCandyName][candy][i] = 1
        } else {
          datesWithCounts[datesByCandyName][candy][i]++
        }
    }
  }
    for (let date in datesWithCounts){
      if (datesWithCounts[date] > most){
        most = datesWithCounts[date]
        result = date
      }
    }
        console.log(datesWithCounts)
  return result
}

Is there a better way to do this? I’m so lost at this point.

I assume you are missing some code, because based on the code down lower, you should have a store2 variable which is assign the object at the top (missing opening/closing curly brackets)?

Yes, but it’s arbitrary, its’ just another file connecting the two. It’s store2.js

No guesses from anyone?

Let’s focus on the code above. See if you can figure out why datesWithCounts[datesByCandyName] is undefined in any given iteration of the inner for loop.

I’m trying to understand why and I’m not getting it.

Put a console.log(datesByCandyName) before the if statement and you might get an idea why.

I’m doing an npm test in the terminal to check and still nothing that I can find.

This is it:
TypeError: Cannot read property ‘Dark Chocolate Crunchies’ of undefined

Put the console.log statement in the code and pug all the code below into the console to run a check:

const store2 = {
  'sale dates': {
    'Dark Chocolate Crunchies': ['2015-01-06', '2015-01-06', '2015-01-06', '2015-01-08'],
    'Mint Wafers': ['2015-01-06', '2015-01-07', '2015-01-07', '2015-01-09'],
    'Peppermint Poppers': ['2015-01-08', '2015-01-08', '2015-01-09', '2015-01-09', '2015-01-10', '2015-01-10', '2015-01-10'],
    'Peanut Butter Buttered Peanuts': ['2015-01-07', '2015-01-07'],
    'Berry Bites': ['2015-01-06', '2015-01-09', '2015-01-09', '2015-01-09', '2015-01-10', '2015-01-10'],
    'Caramel Twists': ['2015-01-06', '2015-01-06', '2015-01-07', '2015-01-08', '2015-01-08', '2015-01-09', '2015-01-09', '2015-01-10', '2015-01-10', '2015-01-10'],
    'Banana Bunches': ['2015-01-10', '2015-01-10']
  }
};

function loopingData2() {
  let most = 0
  let result
  let datesWithCounts = {}
  //access sale dates object
  let datesByCandyName = store2[`sale dates`]
  //get array values
  for (let candy in datesByCandyName) {
    for (let i = 0; i < datesByCandyName[candy].length; i++) {
      console.log(datesByCandyName)
      if (!datesWithCounts[datesByCandyName][candy][i]) {
        datesWithCounts[datesByCandyName][candy][i] = 1
      } else {
        datesWithCounts[datesByCandyName][candy][i]++
      }
    }
  }
  console.log(datesWithCounts)
  return result
}
loopingData2();

if (!datesWithCounts[datesByCandyName][candy][i]) ???

It’s telling me at the bracket of candy

When you write:

datesWithCounts[datesByCandyName]

this is the same object that is in store2[‘sale data’, because you assigned it to datesByCandyName. You need to be referencing candy instead, because candy has the actual candy names which are keys of datesByCandyName.

You meant this?

function loopingData2() {
  let most = 0
  let result
  let datesWithCounts = {}
  //access sale dates object
  let datesByCandyName = store2[`sale dates`]
  //get array values
  for (let candy in datesByCandyName) {
    for (let i = 0; i < candy.length; i++) {
      console.log(datesByCandyName)
      if (!candy[i]) {
        candy[i] = 1
      } else {
        candy[i]++
      }
    }
  }
  return result
}

Nevermind, that’s not it.

In your latest code, what is candy? candy is a string. so candy[i] would be the first letter of the candy name. candy is a property name of the object candies. How do you refer to the value of an object’s property? More specifically, how would you refer to the value of a specific candy in candies?

I was closer before? Sorry, it’s been a very long day and I really want to figure this one out so don’t give up on me! I was at the bootcamp at 6:30AM until 5:30 today.

Current

function loopingData2() {
  let most = 0
  let result
  let datesWithCounts = {}
  //access sale dates object
  let datesByCandyName = store2[`sale dates`]
  //get array values
  for (let candy in datesByCandyName){
    for (let i = 0; i < datesByCandyName[candy].length; i++){
        if(!datesWithCounts[candy][i]){
          datesWithCounts[candy][i] = 1
        } else {
          datesWithCounts[candy][i]++
        }
    }
  }
    for (let date in datesWithCounts){
      if (datesWithCounts[date] > most){
        most = datesWithCounts[date]
        result = date
      }
    }
  return result
}
if(!datesWithCounts[candy][i]){

In the first iteration of the inner for loop, you are attempting to check a property of ‘0’ (because i=0) of a property contained in candy which does not exist yet. In the first iteration of the outer and inner for loops, datesWithCounts[candy] is undefined and undefined does not have any properties.

So I have to do i = something…right? i = datesByCandyName?

CROSS THAT…nevermind.

function loopingData2() {
  let most = 0
  let result
  let datesWithCounts = {}
  //access sale dates object
  let datesByCandyName = store2[`sale dates`]
  //get array values
  for (let candy in datesByCandyName){
    for (let i = 0; i < datesByCandyName.length; i++){
        if(!datesWithCounts[i]){
          datesWithCounts[candy][i] = 1
        } else {
          datesWithCounts[candy][i]++
        }
    }
  }
    for (let date in datesWithCounts){
      if (datesWithCounts[date] > most){
        most = datesWithCounts[date]
        result = date
      }
    }
  return result
}

Just trying it based off of what you’re saying.

Just inside the inner for loop, what if you wrote the following?

let currentDate = datesByCandyName[candy][i];

Now you have a variable which actually contains a date you want to check exists in datesWithCounts.

That does not work, because i will a number like 0, 1, 2 3 and you need to check if a date exists. Look at my last reply.