# CC-12: Objects

with Carmen Salas • 2024/10/23

## Code Challenge

1. Write a function named `stringLengths` that takes in an array of strings as an input, and returns an object, where the keys are all the strings in the array, and the value of each key represents the length of that string.

```js
const stringLengths = (strArr) => {
  let res = {};
  strArray.forEach((str) => res[str] = str.length);
  return res;
}

const instructors = ["Ben", "Motun", "Gonzolo", "Itzel"]
console.log(stringLengths(instructors)) // { Ben: 3, Motun: 5, Gonzolo: 7, Itzel: 5 }
```

2. Write a function named `stringCount` that takes in an array of strings and returns an object where the keys are the strings from the array and the value of each key is a count of how many times the string appears in the object. If there are duplicate strings the values should reflect that.

```js
// for each approach
const stringCount = (strArray) => {
  let res = {};
  strArray.forEach((str) => (res[str]) ? res[str]++ : res[str]=1);
  return res;
}

// reduce approach
const stringCount = (strArray) => {
  return strArray.reduce((res, str) => {
    (res[str]) ? res[str]++ : res[str]=1
    return res;
  }, {});
}

const words = ["apple", "orange", "peach", "pear", "apple"]
console.log(stringCount(words)) // { apple: 2, orange: 1, peach: 1, pear: 1 }
```

{% hint style="info" %}
`forEach` and `reduce` have very similar performance for this function. See [benchmark](https://www.measurethat.net/Benchmarks/Show/32368/0/stringcount-reduce-vs-foreach) here.
{% endhint %}

### BONUS

1. Write a function named `highestFrequencyString` that takes in an array of strings and returns the string with the highest frequency in the array and how many times it appears.

```js
// method mania
const highestFrequencyString = (strArray) => {
  // so janky can we do better?
  let res = {};
  strArray.forEach((str) => (res[str]) ? res[str]++ : res[str]=1);
  // var reuse because i'm a degenerate
  res = Object.entries(res).sort((a,b) => b[1]-a[1])[0];
  return `${res[0]} appears ${res[1]} times in the array.`
}
// for loop because you hate yourself
const highestFrequencyString = (strArray) => {
  strArray.sort((a,b) => a <= b ? -1 : 1);
  let max = 1;
  let curr = 0;
  let res = strArray[0];
  for (let i = 0; i < strArray.length-1; i++) {
    if (strArray[i] === strArray[i+1]) {
      curr++;
    } else {
      if (max < ++curr) {
        max = curr;
        res = strArray[i];
      }
      curr = 0;
    }
  }
  return `${res} appears ${max} times in the array.`
}


const fruits = ["apple", "orange", "apple", "peach", "pear", "apple", "peach"]
console.log(highestFrequencyString(fruits)); // "apple appears 3 times in the array."
```

{% hint style="info" %}
Using purely `Array` and `Object` methods perform very similarly to doing it manually with a `for` loop. See [benchmark](https://www.measurethat.net/Benchmarks/Show/32367/0/highestfrequencystring-2) here.
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://raffycastlee.gitbook.io/marcyannotes/codechallenge-curriculum/unit-2/20241023.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
