I’m a bit curious as to why I am failing check 2 : “If a small straight is rolled, your
checkForStraights function should enable the fourth radio button, set the value to 30, update the displayed text to, score = 30 and leave the fifth radio button disabled.”
Here is the syntax: const checkForStraights = (array) =>{
let straight = array.sort((a,b)=> a - b).filter((num,index)=>{
return array.indexOf(num) === index
}).filter((num,index)=>{
let smallest = array[0]
return smallest + index === num
})
console.log(straight)
if(straight.length >= 4){
updateRadioOption(3,30)
}
if(straight.length === 5){
updateRadioOption(4,40)
}
updateRadioOption(5,0)
}
It creates an array that sorts the dice values and removes duplicates, and then filters based on the smallest number in the array and if the numbers that come after it are consecutive based on the value of the smallest number plus the current index. Based on my current tests, the function works fine but please let me know where I should improve on.
Edit: this array pair ( 4,2,5,3,5) initially didn’t work when using the game functionality, but it works when you call it directly. Why might this be, and should I redefine my if statements to check every rather than length?
Please post a link to the step.
If you have a question about a specific challenge as it relates to your written code for that challenge and need some help, click the Help button located on the challenge. This button only appears if you have tried to submit an answer at least three times.
The Help button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.
Thank you.
Here is the link. I seem to have figured out the problem and its that my code doesn’t account for the scenario of [1,3,4,5,6], since it isn’t consecutive starting at one, despite fitting the requirements for a straight. I’m going to try using a for loop using the index of the current number and checking if the array includes the value of the current number plus the change in index. I think using this and a some should deal with all three small Straight scenarios and both full straight scenarios. If my logic sounds faulty or could use improvement, please let me know and thank you for your suggestions.
Good work.
I would suggest just to use console.log
at the top of the function to find out what inputs the tests are sending, and another console.log
to see how your function handles each input. Then you have solid data to look at.