London Class 9 - Farzaneh Haghani - JavaScript - Week 1 - #443
London Class 9 - Farzaneh Haghani - JavaScript - Week 1#443farzaneh-haghani wants to merge 12 commits into
Conversation
JDysiewicz
left a comment
There was a problem hiding this comment.
Looks good, well done! Hopefully the comment about 8 ball cleared things up a little.
| @@ -1,3 +1,5 @@ | |||
| // Start by creating a variable `greeting` | |||
|
|
|||
| var greeting = "Hello world"; | |||
There was a problem hiding this comment.
try to always use const to declare variables; let can be used too but only use if if the variable needs redefining at some point (normally, this won't be the case, which is why we prefer const).
var is now outdated, so try to avoid using it :P
| console.log(message); | ||
| var myName = "Daniel"; | ||
| var message = myName.length; | ||
| console.log(`My name is ${myName} and my name is ${message} characters long`); |
There was a problem hiding this comment.
nice use of a template string!
| @@ -1,3 +1,5 @@ | |||
| const name = " Daniel "; | |||
|
|
|||
| message = name.trim(" "); | |||
There was a problem hiding this comment.
almost - variables in JS need declaring by prefixing the variable name with let or const. This effectively tells JS "Hey, every time you see variableName from this point onwards, it's actually talking about this variable"
| function divide(x, y) { | ||
| return x / y; | ||
| } |
There was a problem hiding this comment.
You didn't have to account for this, but what would happen if y = 0 was passed in and you end up with x / 0? try doing that in JS and see what happens. With knowledge of conditionals (like if statements) how would you prevent that from occurring?
| let result = add(startingValue, 10); | ||
| result = multiply(result, 2); | ||
| let goodCode = format(result); |
There was a problem hiding this comment.
Yup this is certainly better; one more improvement that could be made here though is to use const instead to declare multiple variables e.g.
const addResult = add(startingValue, 10)
const multiplyResult = multiply(addResult, 2)
const formattedResult = format(multiplyResult)
A nice rule of thumb to go by with code-style is to always err on the more verbose side (i.e. it's almost always better to be more explicit about your intentions). The code will give the same result either way, so all your code-style is for is for other developers who'll look at your code, and in that case it's better to make your intentions very obvious.
| let firstItem = Math.floor(Math.random() * 4); | ||
| let secondItem = Math.floor(Math.random() * 5); |
There was a problem hiding this comment.
So Math.random() returns a random number between 0 and 1 (e.g. 0.6291232). Therefore, when trying to get random integers (i.e. for indexing an array) we tend to multiply by array.length, then get the floor. This means that we generate a random number between 0 and array.length, then get the floor (round down to nearest integer). This way, we generate a random number between 0 - array.length, then get the floor, such that we are guaranteed a random number that is either 0,1,2,3...array.length-1 (remember, the last index of an array is it's length -1 as we start at 0).
You could avoid hard coding the 4 or 5 here by getting the array lengths, but that's beyond what is expected here
Your Details
Homework Details
Notes
What did you find easy? Find syntax errors
What did you find hard? 8 ball magic (It wasn't hard but it was unclear that how it works).
What do you still not understand? Nothing
Any other notes? No
View rendered README.md
View rendered exercises/B-hello-world/README.md