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 trialSam Roberts
1,102 PointsThe video vs. what I wrote. How do I know what the "right" way is if so many variations exist?
The below is what I wrote to complete the challenge. It doesn't look much like what Dave walked us through in the explanation video though. First of all, is the below correct? Secondly, how do I know what the "right" way to code is? It seems like everyone can write differently and get the same output.
var question = parseInt(prompt("Enter any number you would like!"));
var questionTwo = parseInt(prompt("Enter a second number!"));
alert("Now I will give you a number between " + question + " and " + questionTwo);
var dieRoll = Math.floor(Math.random() * questionTwo) + 1;
document.write('Your new number is ' + dieRoll);
1 Answer
Jeffrey Meesters
Full Stack JavaScript Techdegree Graduate 16,657 PointsHi Sam,
Making it work is step 1. Then when progressing trough the tutorial and/or when just coding more and more you'll learn best practices along the way.
Then, making it work is still step 1, and then you'd start refactoring your code to use best practices like:
step 1:
let someVariable = true;
let doIt = ""
if (someVariable) {
doit = "Oke";
} else {
dotit = "Nope"
}
step 2:
let someVariable = true;
let doit = (somevariable) ? "Oke" : "Nope";
So there is not really a wrong way, only a better. And that becomes important when your applications become big in usage and thus your code speed starts to matter, but always make sure your code is readable for humans/co-developers. So when someone else has to work on it that person can understand what you did... or thing about when you have to work on code you wrote months ago... you yourself then needs to understand what you did in the past.