Learn to Code — For Free
So I’ve been trying to solve this step for the past 2 hours and can’t quite understand what I am supposed to do besides what I’ve written, if anyone could point me in the right direction as to why isn’t my code working since I’ve tried it on jsfiddle and it does comply with what the prompt asks me. This is my code so far:
const getMode = (array) => {
const counts = {};
array.forEach((el) => {
if (counts.hasOwnProperty(el)) { //Also tried in arrow form.
counts[el]++; // Setting the count property.
} else {
counts[el] = 1; // Fallback.
}
});
}
I would say that the instructions for this exercise have been somewhat vague and the hints feels lacking in guidance. Other than that I’m very happy with this new beta course. Thank you in advance guys
Were you able to find a solution? I’m stuck on this same step
Nope.
I’m resisting the urge to ask ChatGPT cause I want to solve it by myself .
When I manage to solve it I’ll let you know .
Teller
January 24, 2024, 7:37pm
4
Hi @glovek08
Here’s an article you may find helpful.
Hmm thank you, but still can’t solve it.
For future reference: I modified my original code to also check if counts[el] is a number since .hasOwnProperty returns true even if the property is undefined . So my final if looks like this:
if(counts.hasOwnProperty(el) && !isNaN(counts[el])) {
At this point I’m guessing it’s a bug since it does comply with the prompt. I guess I’ll wait 'till they fix it. Nonetheless if I’m doing something wrong please tell me
Teller
January 24, 2024, 10:40pm
6
Since you are using the forEach method, wouldn’t the array item already exist?
However, your codes look good.
So in theory this code should work?
array.forEach((el) => {
if(counts[el]) {
counts[el]++;
} else {
counts[el] = 1;
}
This was my first code, tested both and they both work,
Just to clarify things, is it me who is not understanding something, or is this step broken?
I believe this step will be fixed to allow any valid solution. The PR isn’t done and it will need to be pushed to production as well.
For now, the solution is fixed and tested using a regex so you have to solve it using that one specific solution. You can jump to the next step to see it as it isn’t super likely that you will land on the exact code the solution requires.
opened 08:07PM - 24 Dec 23 UTC
type: bug
help wanted
status: discussing
scope: curriculum
new javascript course
### Describe the Issue
I would suggest this step is far too terse and expects a… fixed solution when it isn't the only option.
You are not able to log anything, not the array, the element access, or the object after the loop is done, making it fairly impossible to debug anything. I would suggest mocking the data so the camper can access it. That should also let us test the result of the `forEach` without using a regex. There is not only one valid way to solve this making the last regex test questionable.
```
const getMode = (array) => {
console.log(array) // ?
const counts = {};
array.forEach(el => console.log(el)) // ?
array.forEach(el => counts[el] = (counts[el] || 0) + 1)
console.log(counts) // ?
}
```
> In the callback, use the `el` argument to access the `counts` object and increment the count for each number.
This is confusing and strikes me as almost being "inside baseball".
> Use `el` to create a computed property name on the `counts` object. Assign it a default value of `0`, if the property already exists increment it.
There is also a console log in the test code that should be removed.
Tests: https://github.com/freeCodeCamp/freeCodeCamp/blob/main/curriculum/challenges/english/15-javascript-algorithms-and-data-structures-22/learn-advanced-array-methods-by-building-a-statistics-calculator/6352e96d2604f813c656750b.md
### Affected Page
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures-v8/learn-advanced-array-methods-by-building-a-statistics-calculator/step-27
### Your code
Should this not pass?
```
array.forEach((el) => {
if (!counts[el]) {
counts[el] = 0;
}
counts[el] = counts[el] + 1;
});
```
### Expected behavior
1. Explain the requirement better.
2. Work on real data.
3. Do not use regex to test for one expected solution.
### Screenshots
_No response_
### System
- Device: [e.g. iPhone 6, Laptop]
- OS: [e.g. iOS 14, Windows 10, Ubuntu 20.04]
- Browser: [e.g. Chrome, Safari]
- Version: [e.g. 22]
### Additional context
_No response_
freeCodeCamp:main ← weilirs:multiple-issue
opened 06:24PM - 15 Jan 24 UTC
Checklist:
- [x] I have read and followed the [contribution guidelines](h… ttps://contribute.freecodecamp.org).
- [x] I have read and followed the [how to open a pull request guide](https://contribute.freecodecamp.org/#/how-to-open-a-pull-request).
- [x] My pull request targets the `main` branch of freeCodeCamp.
- [x] I have tested these changes either locally on my machine, or GitPod.
Closes [#52753](https://github.com/freeCodeCamp/freeCodeCamp/issues/52753)
2 Likes
I am completely lost here too. What does it want us to do? Is this a bug?
At my current level of knowledge I would never have been able to solve it thanks for the reply
lasjorg
January 26, 2024, 12:01am
11
I would suggest you skip ahead (use the browser address bar and go to step 28) then just look at the solution code and go back and write it.
I would consider it a bug, but really it is just too strict a test.
2 Likes
use || as a fallback mechanism instead of if statements.
Mod edit: solution code removed
I had the same issue, although my solution was ok, it was:
array.forEach((el) => counts[el] = counts[el] ? counts[el] +=1 : 1 )
But as @ritesh.poudel.34 already posted, that’s the official solution and the only one the test will be happy on.
Please do not post the solution code.
If anyone wants the solution they can skip forward. But it should not be posted on the forum.
This step is being worked on and should get updated at some point.
1 Like
system
Closed
September 28, 2024, 6:14am
16
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.