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 trialJoshua Vandergriff
Full Stack JavaScript Techdegree Student 1,898 PointsError w/ a Globally scoped variable
Hey there everyone, hope everyone's having an excellent coding journey! I was curious if anyone could answer this quick question. I am working on the Random Number Function challenge here. I got the top half to work where there are 2 prompt boxes that appear, collect a number (comes in as a string) and I convert them to numbers, no issues there. However, when I try to then use numbers gathered in my prompt functions it doesn't let me and I get back the error SyntaxError: Identifier 'numOne' has already been declared
. Since I declarednumOne
& numTwo
outside of my arrow function & above it, aren't they considered to be globally scoped and should be accessible as arguments when I call my randomNumberGen
function? My goal here was to take in information from the user, run it through the function, and dynamically display it back on the webpage (I haven't wrote out the query selector yet, but that's the goal and that part I know how to do).
let numOne = prompt("Think of a number between 1 and 5");
numOne = parseInt(numOne);
let numTwo = prompt("Think of a new number between 1 and 5");
numTwo = parseInt(numTwo);
let randomNumberGen = (min, max) => {
const rndInt = Math.floor(Math.random() * (max - min) + min);
};
randomNumberGen(numOne, numTwo);
1 Answer
Steven Parker
231,269 PointsThe code shown here should not produce that error. I might expect that error if there had been a stray extra "let" at the beginning of line 2.
But it's odd that randomNumberGen computes a number and stores it in a local variable but doesn't return anything. And if you're not going to do anything else to it in the function, you can skip creating the variable entirely:
return Math.floor(Math.random() * (max - min) + min);