-
-
Notifications
You must be signed in to change notification settings - Fork 475
London Class 9 - Farzaneh Haghani - JavaScript - Week 1 #443
base: master
Are you sure you want to change the base?
Changes from all commits
61bc11b
8bb08bc
33841b4
588d1fe
6b01c2d
69c27b0
0863da5
6a54bcd
4f62691
3ab14ac
6643f6f
c453afb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| console.log("Hello world"); | ||
| console.log("Hello world, I just started learning JavaScript!"); | ||
| console.log(54); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| // Start by creating a variable `greeting` | ||
|
|
||
| var greeting = "Hello world"; | ||
| console.log(greeting); | ||
| console.log(greeting); | ||
| console.log(greeting); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| // Start by creating a variable `message` | ||
|
|
||
| var message = "This is a string"; | ||
| console.log(message); | ||
| console.log(typeof message); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| // Start by creating a variable `message` | ||
|
|
||
| var greeting = "Hello, my name is "; | ||
| var myName = "Daniel"; | ||
| message = greeting + myName; | ||
| console.log(message); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| // Start by creating a variable `message` | ||
|
|
||
| console.log(message); | ||
| var myName = "Daniel"; | ||
| var message = myName.length; | ||
| console.log(`My name is ${myName} and my name is ${message} characters long`); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice use of a template string! |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| const name = " Daniel "; | ||
|
|
||
| message = name.trim(" "); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. almost - variables in JS need declaring by prefixing the variable name with |
||
| console.log(message); | ||
| console.log(`My name is ${message} and my name is ${message.length} characters long`); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,7 @@ | ||
| // Start by creating a variables `numberOfStudents` and `numberOfMentors` | ||
| var numberOfStudents = 15; | ||
| var numberOfMentors = 8; | ||
| var total = numberOfMentors + numberOfStudents; | ||
| console.log(`Number of students: ${numberOfStudents}`); | ||
| console.log(`Number of mentors: ${numberOfMentors}`); | ||
| console.log(`Total number of students and mentors: ${total}`); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,8 @@ | ||
| var numberOfStudents = 15; | ||
| var numberOfMentors = 8; | ||
| var total = numberOfMentors + numberOfStudents; | ||
| var percentageOfStudents = (numberOfStudents * 100) / total; | ||
| var percentageOfMentors = (numberOfMentors * 100) / total; | ||
|
|
||
| console.log(`Percentage students: ${Math.round(percentageOfStudents)}%`); | ||
| console.log(`Percentage mentors: ${Math.round(percentageOfMentors)}%`); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,14 @@ | ||
| function halve(number) { | ||
| // complete the function here | ||
| return number / 2; | ||
| } | ||
|
|
||
| var result = halve(12); | ||
| console.log(result); | ||
|
|
||
| var result = halve(20); | ||
| console.log(result); | ||
|
|
||
| var result = halve(36); | ||
| console.log(result); | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,9 @@ | ||
| function triple(number) { | ||
| // complete function here | ||
| return number * 3; | ||
| } | ||
|
|
||
| var result = triple(12); | ||
|
|
||
| console.log(result); | ||
|
|
||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| // Declare your function first | ||
|
|
||
| function add(x, y) { | ||
| return x + y; | ||
| } | ||
| // Call the function and assign to a variable `sum` | ||
|
|
||
| var sum = add(13, 124); | ||
| console.log(sum); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| // Declare your function here | ||
|
|
||
| function createLongGreeting(name, age) { | ||
| return `Hello, my name is ${name} and I'm ${age} years old`; | ||
| } | ||
| const greeting = createLongGreeting("Daniel", 30); | ||
|
|
||
| console.log(greeting); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,30 +16,31 @@ | |
| the final result to the variable goodCode | ||
| */ | ||
|
|
||
| function add() { | ||
|
|
||
| function add(firstNum, secondNum) { | ||
| return firstNum + secondNum; | ||
| } | ||
|
|
||
| function multiply() { | ||
|
|
||
| function multiply(firstNum, secondNum) { | ||
| return firstNum * secondNum; | ||
| } | ||
|
|
||
| function format() { | ||
|
|
||
| function format(num) { | ||
| return `£${num}`; | ||
| } | ||
|
|
||
| const startingValue = 2; | ||
|
|
||
| // Why can this code be seen as bad practice? Comment your answer. | ||
| let badCode = | ||
| let badCode = format(multiply(add(startingValue, 10), 2)); | ||
|
|
||
| /* BETTER PRACTICE */ | ||
|
|
||
| let goodCode = | ||
| let result = add(startingValue, 10); | ||
| result = multiply(result, 2); | ||
| let goodCode = format(result); | ||
|
Comment on lines
+37
to
+39
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup this is certainly better; one more improvement that could be made here though is to use 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. |
||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== | ||
| There are some Tests in this file that will help you work out if your code is working. | ||
|
|
||
| To run the tests for just this one file, type `npm test -- --testPathPattern 2-piping` into your terminal | ||
| (Reminder: You must have run `npm install` one time before this will work!) | ||
| */ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
try to always use
constto declare variables;letcan 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 preferconst).varis now outdated, so try to avoid using it :P