Closed mahesha1150 closed 11 months ago
The latest updates on your projects. Learn more about Vercel for Git ↗︎
Name | Status | Preview | Comments | Updated (UTC) |
---|---|---|---|---|
some-drops-of-javascript | ✅ Ready (Inspect) | Visit Preview | 💬 Add feedback | Oct 26, 2023 9:59am |
Amazing pull request @mahesha1150 . Thank you. Can i ask you (if you have time and you want ) to add another example (in the script in the examples directory) and in the markdown file. I would like to show how to count the occorrences in an aray. Something similar to this one:
const fruits = ['🍓', '🍋', '🍋', '🍎', '🍋', '🍓']
const fruitCounter = fruits.reduce((accumulator, currentFruit) => {
accumulator[currentFruit] = (accumulator[currentFruit] || 0) + 1
return accumulator
}, {})
console.log(fruitCounter)
the result is:
{
"🍓": 2,
"🍋": 3,
"🍎": 1
}
Sure Roberto. While researching I have found additional examples as below in addition to the above one:
Finding the Maximum and Minimum Values: const numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5];
const max = numbers.reduce((accumulator, currentValue) => { return Math.max(accumulator, currentValue); });
const min = numbers.reduce((accumulator, currentValue) => { return Math.min(accumulator, currentValue); });
console.log("Maximum value:", max); // Output: Maximum value: 9 console.log("Minimum value:", min); // Output: Minimum value: 1
Transforming an Array into a Different Data Structure: const data = [ { id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }, { id: 3, name: 'Charlie' }, ];
const transformedData = data.reduce((accumulator, currentValue) => { accumulator[currentValue.id] = currentValue.name; return accumulator; }, {});
console.log("Transformed Data:", transformedData); // Output: Transformed Data: { 1: 'Alice', 2: 'Bob', 3: 'Charlie' }
But these additional two examples will be making .md file heavy when viewing in browser. So I can skip these two and add only how to count the occurrences in an array
As mentioned in the above comment, have added additional examples only in example file. Please check and let me know if this is fine.
Also not sure, why Hactoberfest team did not accept this PR on reduce().
merged and flagged as hacktoberfest-accepted
Thank You @roberto-butti.
Please let me know, if there is anything I can help with.
HI @roberto-butti, Please review the pull request and let me know if any changes have to be made.