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.

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

No guesses from anyone?

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

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

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

It’s telling me at the bracket of candy

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.

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
}

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.

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++){
      let currentDate = datesByCandyName[candy][i]
        if(!datesWithCounts[currentDate][i]){
          datesWithCounts[currentDate][i] = 1
        } else {
          datesWithCounts[currentDate][i]++
        }
    }
  }
    for (let date in datesWithCounts){
      if (datesWithCounts[date] > most){
        most = datesWithCounts[date]
        result = date
      }
    }
  return result
}

What sucks is just not understanding.

It passed thank you…I’m going to try and comment it as best as I can so I understand what is happening.

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++){
      let currentDate = datesByCandyName[candy][i]
        if(!datesWithCounts[currentDate]){
          datesWithCounts[currentDate] = 1
        } else {
          datesWithCounts[currentDate]++
        }
    }
  }
    for (let date in datesWithCounts){
      if (datesWithCounts[date] > most){
        most = datesWithCounts[date]
        result = date
      }
    }
  return result
}

That’s what I’m confused about…for…of loops. I thought best practice is to used for…in loops for objects?

Almost done with the first week of the bootcamp…even when I struggle this bad, someone like me can seriously get to your type of efficiency? I get really down on myself a lot because I’m struggling so much.

Thanks as always @camperextraordinaire. Truly.

Yeah, it was far too simple. I was overthinking.

I commented and refactored a little. Do these comments do justice to what is actually happening?

function loopingData2() {
  //date result to be returned
  let result
  //counter to keep track of most frequent date
  let most = 0

  //empty object to store incremented value
  let countedDates = {}
  //access `sale dates` object
  let candies = store2[`sale dates`]
  //get array values
  for (let key in candies){
    //loop through every value (dates) in `sale dates` object
    for (let i = 0; i < candies[key].length; i++){
      //store incremented values
      let currentDate = candies[key][i]
      //if the incremented value is there, increment by one
        if(countedDates[currentDate]){
          countedDates[currentDate]++
        } else {
          //else equal it to 1
          countedDates[currentDate] = 1
        }
    }
  }
    //block of code to check which has the most frequency
    //for every date in our object
    for (let date in countedDates){
      //if the frequency is greater than 0
      if (countedDates[date] > most){
        //set most to that frequency
        most = countedDates[date]
        //set the most frequent date to the result
        result = date
      }
    }
  return result
}

@camperextraordinaire You are amazing, thank you, but just still being a newbie and a week into the bootcamp, I was asking if code like this is normal to get through a problem and then have it for understanding later. That’s all. As far as professional code? I wasn’t really expecting to hit that bar yet. :wink: