Skip to content

Commit 88844af

Browse files
committed
feat(blog): more progress with new blog post
1 parent 296da26 commit 88844af

File tree

2 files changed

+24
-17
lines changed

2 files changed

+24
-17
lines changed

src/app/(marketing)/blog/posts/how-to-use-reduce-in-javascript.mdx

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -128,32 +128,41 @@ Let's break down this example step by step:
128128

129129
<div className="my-4">
130130

131-
1. First, we start with an array of numbers that has some duplicates:
131+
1. We start with an array of numbers that contains some duplicates:
132132
<CodeSnippet language="javascript" content={`const numbers = [1, 2, 2, 3, 3, 3, 4, 4, 5];`} />
133133

134-
2. We use reduce with an empty object `{}` as the initial value:
134+
2. We initialize reduce with an empty object as our accumulator:
135135
<CodeSnippet language="javascript" content={`const frequency = numbers.reduce((count, num) => {}, {});`} />
136136

137-
3. For each number in the array, we:
138-
For each number in the array, we:
139-
- Check if the number exists as a key in our object using `count[num]`
140-
- If it doesn't exist, use `0` as the default value with `|| 0`
141-
- Add 1 to the count
142-
- Return the updated count object
143-
144-
4. Let's see how the object changes in each iteration:
145-
146-
147-
5. The key part of this code is the line:
137+
3. In each iteration, we:
138+
- Use the current number as a key in our object (`count[num]`)
139+
- If this number hasn't been seen before, start its count at 0
140+
- Add 1 to the count for this number
141+
- Return the updated count object for the next iteration
142+
143+
4. Let's walk through how the object is built:
144+
- Initial: `{}`
145+
- After 1: `{ 1: 1 }`
146+
- After 2: `{ 1: 1, 2: 1 }`
147+
- After 2: `{ 1: 1, 2: 2 }`
148+
- After 3: `{ 1: 1, 2: 2, 3: 1 }`
149+
- And so on...
150+
151+
5. The magic happens in this line:
148152
<CodeSnippet language="javascript" content={`count[num] = (count[num] || 0) + 1;`} />
149-
This is a shorthand way of writing:
153+
154+
Which is a shorter way of writing:
150155
<CodeSnippet language="javascript" content={`if (count[num] === undefined) {
151156
count[num] = 1;
152157
} else {
153158
count[num] = count[num] + 1;
154159
}`} />
155160

156-
This pattern is commonly used when you need to count occurrences in an array or create frequency maps, such as in analytics, data processing, or character frequency counting in strings.
161+
This technique is incredibly useful when you need to count occurrences in data. You might use it to:
162+
- Track user interactions in analytics
163+
- Count character frequencies in strings
164+
- Analyze voting or survey results
165+
- Generate statistics from data sets
157166
</div>
158167

159168

src/components/global/random-question.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ export default async function RandomQuestion(opts: {
2323
currentQuestionSlug,
2424
});
2525

26-
console.log('randomQuestion', randomQuestion);
27-
2826
redirect(`/question/${randomQuestion}`);
2927
}}
3028
>

0 commit comments

Comments
 (0)