Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(curriculum): last test for step 1 RPS project to better check for final result #54815

Merged

Conversation

jdwilkin4
Copy link
Contributor

@jdwilkin4 jdwilkin4 commented May 15, 2024

The last test for the Rock, Paper, Scissors game review project allows for incorrect answers like this to pass

function getRandomComputerResult() {
  const options = ["Rock", "Paper", "Scissors"];
  return "Paper"
}

this update should better test for the correct final result so hardcoded solutions like this don't pass

Checklist:

Closes #54814

@jdwilkin4 jdwilkin4 self-assigned this May 15, 2024
@github-actions github-actions bot added the scope: curriculum Lessons, Challenges, Projects and other Curricular Content in curriculum directory. label May 15, 2024
@jdwilkin4 jdwilkin4 added new javascript course These are for issues dealing with the new JS curriculum status: waiting review To be applied to PR's that are ready for QA, especially when additional review is pending. and removed scope: curriculum Lessons, Challenges, Projects and other Curricular Content in curriculum directory. labels May 15, 2024
Copy link
Contributor

@JoyShaheb JoyShaheb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i approve the solution 👍

@lasjorg
Copy link
Contributor

lasjorg commented May 18, 2024

It seems like it is also possible to pass this with an incorrect index that gives back undefined.

return options[Math.floor(Math.random() * 3) + 1]

Obviously, getting it to pass on the first try is...well, random.

test-passing

@github-actions github-actions bot added the scope: curriculum Lessons, Challenges, Projects and other Curricular Content in curriculum directory. label May 20, 2024
@lasjorg
Copy link
Contributor

lasjorg commented May 20, 2024

I'm not sure if we should specifically have a regex test for that. If anything, I think it should test for valid random generation and not invalid.

It seems fairly improbable that the Set wouldn't contain all three strings after calling the function 50 times. So how about we just check the Set contains what we expect it to?

I guess we can also remove falsy values and check the length, but I'm not sure if there is any benefit to that over just checking for the expected strings.

@jdwilkin4
Copy link
Contributor Author

@lasjorg

So how about we just check the Set contains what we expect it to?

I am not sure I am understanding what you mean here.
Because we already have this test, to check to make sure their answer returns one of the valid random choices

Your `getRandomComputerResult` function should return one of the options in the `options` array.
```js
assert.include(["Rock", "Paper", "Scissors"], getRandomComputerResult());
```

So wouldn't it be redundant to do another assert.includes here for the last test?

@lasjorg
Copy link
Contributor

lasjorg commented May 20, 2024

That test checks that calling the function one time produces at least one of the 3 strings in the array.

The other test calls it 50 times to "guaranty" the function produces each of the three strings at least one time.

@lasjorg
Copy link
Contributor

lasjorg commented May 20, 2024

function getRandomComputerResult() {
  const options = ["Rock", "Paper", "Scissors"];
  return options[Math.floor(Math.random() * 3)]
}

const results = new Set();

for (let i = 0; i < 50; i++) {
  results.add(getRandomComputerResult());
}

console.log([...results].filter(Boolean).length === 3); // true

function getRandomComputerResult() {
  const options = ["Rock", "Paper", "Scissors"];
  return options[Math.floor(Math.random() * 3 + 1)]
}

const results = new Set();

for (let i = 0; i < 50; i++) {
  results.add(getRandomComputerResult());
}

console.log([...results].filter(Boolean).length === 3); // false

@jdwilkin4
Copy link
Contributor Author

I had reached out to Naomi and went with her suggestion.
Tested it locally and it works on my end.
It looks like both your's and Naomi's suggestion are pretty similar

@lasjorg
Copy link
Contributor

lasjorg commented May 20, 2024

That works, I wasn't really suggesting anything, just showing what I meant in code.

But do we really need to loop that many times?

I'm no statistician, but even with 50 loops the probability of Math.floor(Math.random() * 3) not generating all three number in the range is very low.

Edit: I know it is quick on a desktop, but for low power devices it might be a little overkill. Just a thought.

@jdwilkin4
Copy link
Contributor Author

Yeah, that makes sense.
I updated it to be run 50 times

Copy link
Contributor

@lasjorg lasjorg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me.

We could use 100 or even 200 for the loop, but I think 50 is just fine.

@jdwilkin4 jdwilkin4 merged commit b19a58c into freeCodeCamp:main May 21, 2024
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new javascript course These are for issues dealing with the new JS curriculum scope: curriculum Lessons, Challenges, Projects and other Curricular Content in curriculum directory. status: waiting review To be applied to PR's that are ready for QA, especially when additional review is pending.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Step 1 of RPS project allows for incorrect answers
3 participants