-
-
Notifications
You must be signed in to change notification settings - Fork 475
Northwest Class 4 - Humail Khan - JavaScript-Core-1 - Coursework - Week 1 #115
Changes from all commits
6466854
aac0473
fe1a980
a9cc88c
b614a3a
845b7b4
b6bb4d0
4923c33
7431455
577c13e
c904d86
4868a4b
10d2c3e
9e0181e
bd7e90f
43ab58b
96fc414
397457f
a4df901
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,3 @@ | ||
| console.log("Hello world"); | ||
| console.log("Hello World. I just started learning JavaScript!"); | ||
| console.log(12); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,6 @@ | ||
| // Start by creating a variable `greeting` | ||
| const 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,6 @@ | ||
| // Start by creating a variable `message` | ||
| const myMessage = "Hi, I am Humail"; | ||
| const myMessageType = typeof myMessage; | ||
|
|
||
| console.log(message); | ||
| console.log(myMessage); | ||
| console.log(myMessageType); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,7 @@ | ||
| // Start by creating a variable `message` | ||
| const greetingPartOne = "Hi, my name is "; | ||
| const firstName = "Humail"; | ||
|
|
||
| console.log(message); | ||
| const greetingConcatenated = greetingPartOne + firstName; | ||
|
|
||
| console.log(greetingConcatenated); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,11 @@ | ||
| // Start by creating a variable `message` | ||
| const firstName = "Humail"; | ||
| const lengthOfMyName = firstName.length; | ||
| const message = | ||
| "My name is " + | ||
| firstName + | ||
| " and my name is " + | ||
| lengthOfMyName + | ||
| " characters long."; | ||
|
|
||
| console.log(message); | ||
| console.log(message.trim()); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,10 @@ | ||
| const name = " Daniel "; | ||
| const lengthOfMyName = name.trim().length; | ||
| const message = | ||
| "My name is " + | ||
| name + | ||
| " and my name is " + | ||
| lengthOfMyName + | ||
| " characters long."; | ||
|
|
||
| console.log(message); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,8 @@ | ||
| // Start by creating a variables `numberOfStudents` and `numberOfMentors` | ||
| const numberOfStudents = 15; | ||
| const numberOfMentors = 8; | ||
| const totalNumbers = numberOfStudents + numberOfMentors; | ||
|
|
||
| console.log(`Number of students: ${numberOfStudents}`); | ||
| console.log(`Number of mentors: ${numberOfMentors}`); | ||
| console.log(`Total number of students and mentors: ${totalNumbers}`); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,8 @@ | ||
| var numberOfStudents = 15; | ||
| var numberOfMentors = 8; | ||
| let totalNumbers = numberOfStudents + numberOfMentors; | ||
| let percentageStudents = (numberOfStudents / totalNumbers) * 100; | ||
| let percentageMentors = (numberOfMentors / totalNumbers) * 100; | ||
|
|
||
| console.log(`Percentage students: ${Math.round(percentageStudents)}%`); | ||
| console.log(`Percentage mentors: ${Math.round(percentageMentors)}%`); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,12 @@ | ||
| function halve(number) { | ||
| // complete the function here | ||
| return number / 2; | ||
| } | ||
|
|
||
| var result = halve(12); | ||
|
|
||
| console.log(result); | ||
|
|
||
| let resultTwo = halve(19); | ||
|
|
||
| console.log(resultTwo); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| function triple(number) { | ||
| // complete function here | ||
| return number * 3; | ||
| } | ||
|
|
||
| var result = triple(12); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,7 @@ | ||
| // Declare your function first | ||
| function divide(num1, num2) { | ||
| return num1 / num2; | ||
| } | ||
|
|
||
| var result = divide(3, 4); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| // Declare your function first | ||
|
|
||
| function addingNumbers(num1, num2) { | ||
| return num1 + num2; | ||
| } | ||
| // Call the function and assign to a variable `sum` | ||
|
|
||
| let sum = addingNumbers(13, 124); | ||
| console.log(sum); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,26 +16,27 @@ | |
| the final result to the variable goodCode | ||
| */ | ||
|
|
||
| function add() { | ||
|
|
||
| function add(num1, num2) { | ||
| return num1 + num2; | ||
| } | ||
|
|
||
| function multiply() { | ||
|
|
||
| function multiply(a, b) { | ||
| return a * b; | ||
| } | ||
|
|
||
| function format() { | ||
|
|
||
| function format(amount) { | ||
| return `£${parseFloat(amount.toFixed(1))}`; | ||
| } | ||
|
|
||
| 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)); | ||
|
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. Can you articulate why this code would be bad practice?
Author
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. My take on this one was that chaining too many times would be considered as bad practice. |
||
|
|
||
| /* BETTER PRACTICE */ | ||
|
|
||
| let goodCode = | ||
| let addedNumbers = add(startingValue, 10); | ||
| let multipliedNumbers = multiply(addedNumbers, 2); | ||
| let goodCode = format(multipliedNumbers); | ||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== | ||
| There are some Tests in this file that will help you work out if your code is working. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,11 +43,52 @@ | |
| Very doubtful. | ||
| */ | ||
|
|
||
| const veryPositive = [ | ||
| "It is certain.", | ||
| "It is decidedly so.", | ||
| "Without a doubt.", | ||
| "Yes - definitely.", | ||
| "You may rely on it.", | ||
| ]; | ||
|
|
||
| const positive = [ | ||
| "As I see it, yes.", | ||
| "Most likely.", | ||
| "Outlook good.", | ||
| "Yes.", | ||
| "Signs point to yes.", | ||
| ]; | ||
|
|
||
| const negative = [ | ||
| "Reply hazy, try again.", | ||
| "Ask again later.", | ||
| "Better not tell you now.", | ||
| "Cannot predict now.", | ||
| "Concentrate and ask again.", | ||
| ]; | ||
|
|
||
| const veryNegative = [ | ||
| "Don't count on it.", | ||
| "My reply is no.", | ||
| "My sources say no.", | ||
| "Outlook not so good.", | ||
| "Very doubtful.", | ||
| ]; | ||
|
|
||
| const allAnswers = veryPositive.concat(positive, negative, veryNegative); | ||
|
|
||
| // This should log "The ball has shaken!" | ||
| // and return the answer. | ||
| function shakeBall() { | ||
| //Write your code in here | ||
|
|
||
| console.log("The ball has shaken!"); | ||
|
|
||
| const answerIndex = Math.floor(Math.random() * allAnswers.length); | ||
|
|
||
| return allAnswers[answerIndex]; | ||
| } | ||
| let ans = shakeBall(); | ||
|
|
||
| /* | ||
| This function should say whether the answer it is given is | ||
|
|
@@ -58,9 +99,21 @@ function shakeBall() { | |
|
|
||
| This function should expect to be called with any value which was returned by the shakeBall function. | ||
| */ | ||
|
|
||
| function checkAnswer(answer) { | ||
| //Write your code in here | ||
|
|
||
| if (veryPositive.includes(answer)) { | ||
| return "very positive"; | ||
| } else if (positive.includes(answer)) { | ||
| return "positive"; | ||
| } else if (negative.includes(answer)) { | ||
| return "negative"; | ||
| } else if (veryNegative.includes(answer)) { | ||
| return "very negative"; | ||
|
Comment on lines
+105
to
+113
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 - this is a good use case for the array method "includes"
Author
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. Thank you Paul. This was refactored with the help of Guardians. |
||
| } | ||
| } | ||
| checkAnswer(ans); | ||
|
|
||
| /* | ||
| ================================== | ||
|
|
@@ -101,7 +154,9 @@ test("magic 8 ball returns different values each time", () => { | |
| ); | ||
| } | ||
|
|
||
| let seenPositivities = new Set(Array.from(seenAnswers.values()).map(checkAnswer)); | ||
| let seenPositivities = new Set( | ||
| Array.from(seenAnswers.values()).map(checkAnswer) | ||
| ); | ||
| if (seenPositivities.size < 2) { | ||
| throw Error( | ||
| "Expected to random answers with different positivities each time shakeBall was called, but always got the same one" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,20 @@ | ||
| // Add comments to explain what this function does. You're meant to use Google! | ||
| // Whenever this function is called, it generates a random number and multiplies it by 10. | ||
| function getRandomNumber() { | ||
| return Math.random() * 10; | ||
| } | ||
|
|
||
| // Add comments to explain what this function does. You're meant to use Google! | ||
| //This function takes two parameters which are strings and concatenates them into one string. | ||
| function combine2Words(word1, word2) { | ||
| return word1.concat(word2); | ||
| } | ||
|
|
||
| function concatenate(firstWord, secondWord, thirdWord) { | ||
| // Write the body of this function to concatenate three words together. | ||
| // Look at the test case below to understand what this function is expected to return. | ||
| // We could not use concat() because it only accepts strings. It would not meet all conditions of the test. | ||
| return firstWord + " " + secondWord + " " + thirdWord; | ||
|
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. How would you explain what happens in this function in order for the last test case to work with a number? Google "javascript coercion" if you'd like to find out more! We'll see more examples of this throughout the course.
Author
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. I would have to look it up. But my guess is that it has to do something with adding numbers and strings together. 13 is a number in the last test and the rest are strings. Probably the typeof would be different and they would be added together but not computed? I will look into javascript coercion. Thanks Paul. |
||
| } | ||
|
|
||
| /* | ||
|
|
||
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.
nice use of string template
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.
Thank you.