Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Is it possible to get this to work with an arrow function syntax? I tried and the code simply failed to work =/

Running the code as is in the video works. But the below code fails to run, even though I believe it is written correctly, syntax wise:

let randomNumber = getRandomNumber(10); let guess; let guessCount = 0; let correctGuess = false; //often call this a "Flag" to wave for logic // //function getRandomNumber(upper) { // let num = Math.floor(Math.random() * upper) + 1; // return num; //}

const getRandomNumber = (10) => { let num = Math.floor(Math.random() * upper) + 1; return num; }

do { guess = prompt('I am thinking of a number between 1 and 10. What is it?'); guessCount += 1; if (parseInt(guess) === randomNumber) { correctGuess = true; } } while (!correctGuess) document.write('<h1>You guessed the correct number!</h1>'); document.write('And to think...it only took you ' + guessCount + ' tries to guess ' + randomNumber + ' ^_^');

Please let me know your thoughts. Thank you!

1 Answer

Steven Parker
Steven Parker
231,141 Points

Conventional functions are "hoisted", which allows you to call them before they are defined. Arrow functions always must be defined first.

Then, in the definition itself, the parameter name "upper" should appear in the parentheses instead of the value 10. The value will be supplied when the function is called.