Hi @g2431783256, thanks for sharing your code . Check your generateTarget function. Currently it is returning 0 - 8 but I believe it should be returning 0 - 9. Also, in your compareGuesses function, remember to check the case where human and computer tie. Cheers!
Nice work @mjwakex. I see you wrote your own absolute distance logic! I think it would help the readability if you extracted that logic to its own getAbsoluteDistance function and called that from within compareGuess. Also, in your generateTarget function, you could try returning the random number immediately instead of assigning it to a variable first. Looking forward to seeing more of your work.
Here’s my solution. I have finished both extra exercises.
The problem I had encountered was no windows showing up when input value is smaller than zero or larger than nine.
I finally figured out that the function alert() I defined might be confused by the build-in syntax, alert(), of JavaScript.
In JavaScript, we can use window.alert() to have an alert box popped up, but this window.alert() can also be written as alert(), which is identical to the function name I defined.
After realized this difference, I changed the name of my function, and every thing works.
I feel like I’m missing something because when I try to play the game on my browser, nothing happens. I enter my number but it doesn’t show a target number or computer guess. I’m not sure what’s going wrong or if it’s just my browser?
Here is my solution here.
About Step 4 is quite interesting. Despite the seeing multiple people able to refactor it to a ternary statement, and the logic make sense too, however, if I try with my code, the computer became the ultimate winner. Does any one have some thought about that?
Its because the compareGuesses() is not doing anything. if(human === computer){
return true; her you are returning true to nowhere(no out put because these are not being compare to the ‘closest target’)
here the way you are trying to get the abs value is not correct. the statement after || is to get the closest value yet its not being compared to either human or computer’s guess. its not returning a value too.
here is how I tried to do it. there might be a better way of doing it but this works.
I’m having trouble understanding how the generateTarget() function links to a value for the target parameter in the compareGuesses() function. It seems in the solution there is no relationship between generateTarget() and compareGuesses(), but the code runs. How is this?
Hello, i am a complete beginner and I have a lot of confusions on how to run codes on VS Code.
I’ve the basic code on point for this project but I’m not able to run it in my browser.
It works okay but I have one detail I think I missed. Because when I try to input a number larger than nine, I was able to code an alert to show the player to pick number from 0-9 only, but the computer would still automatically win and get a point. I wonder if there is something I can do in the code to prevent this behavior? Thanks!