-
-
Notifications
You must be signed in to change notification settings - Fork 476
LONDON_10 | SAIM KORKMAZ | JS-1-1 #510
base: master
Are you sure you want to change the base?
Changes from all commits
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 |
|---|---|---|
|
|
@@ -47,8 +47,33 @@ | |
| // and return the answer. | ||
| function shakeBall() { | ||
| //Write your code in here | ||
| } | ||
|
|
||
| const possibleAnswers = [ | ||
| "It is certain.", | ||
| "It is decidedly so.", | ||
| "Without a doubt.", | ||
| "Yes - definitely.", | ||
| "You may rely on it.", | ||
| "As I see it, yes.", | ||
| "Most likely.", | ||
| "Outlook good.", | ||
| "Yes.", | ||
| "Signs point to yes.", | ||
| "Reply hazy, try again.", | ||
| "Ask again later.", | ||
| "Better not tell you now.", | ||
| "Cannot predict now.", | ||
| "Concentrate and ask again.", | ||
| "Don't count on it.", | ||
| "My reply is no.", | ||
| "My sources say no.", | ||
| "Outlook not so good.", | ||
| "Very doubtful.", | ||
| ]; | ||
| const randomIndex = Math.floor(Math.random() * possibleAnswers.length); | ||
| console.log("The ball has shaken!"); | ||
| return possibleAnswers[randomIndex]; | ||
| } | ||
| /* | ||
| This function should say whether the answer it is given is | ||
| - very positive | ||
|
|
@@ -60,6 +85,34 @@ function shakeBall() { | |
| */ | ||
| function checkAnswer(answer) { | ||
| //Write your code in here | ||
|
|
||
| if ( | ||
| answer == "It is certain." || | ||
| answer == "It is decidedly so." || | ||
| answer == "Without a doubt." || | ||
| answer == "Yes - definitely." || | ||
| answer == "You may rely on it." | ||
| ) { | ||
| return "very positive"; | ||
| } else if ( | ||
| answer == "As I see it, yes." || | ||
| answer == "Most likely." || | ||
| answer == "Outlook good." || | ||
| answer == "Yes." || | ||
| answer == "Signs point to yes." | ||
| ) { | ||
| return "positive"; | ||
| } else if ( | ||
| answer == "Reply hazy, try again." || | ||
| answer == "Ask again later." || | ||
| answer == "Better not tell you now." || | ||
| answer == "Cannot predict now." || | ||
| answer == "Concentrate and ask again." | ||
| ) { | ||
| return "negative"; | ||
| } else { | ||
| return "very negative"; | ||
| } | ||
|
Comment on lines
+89
to
+115
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. This is fine, but you are using a lot of magic words here. Try to look into what magic words are and how this could be improved. Hint: |
||
| } | ||
|
|
||
| /* | ||
|
|
@@ -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,16 @@ | ||
| // There are syntax errors in this code - can you fix it to pass the tests? | ||
|
|
||
| function addNumbers(a b c) { | ||
| function addNumbers(a, b, c) { | ||
| return a + b + c; | ||
| } | ||
|
|
||
| function introduceMe(name, age) | ||
| return `Hello, my {name}` is "and I am $age years old`; | ||
|
|
||
| function introduceMe(name, age) { | ||
| return `Hello, my name is ${name} and I am ${age} years old`; | ||
| } | ||
| function getTotal(a, b) { | ||
| total = a ++ b; | ||
| total = a + b; | ||
|
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. This is missing a declaration, could you explain why it worked without a
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 made it global variable by mistake, but has a side effect it is it may cause unexpected behaviour |
||
|
|
||
| return "The total is total"; | ||
| return "The total is " + total; | ||
| } | ||
|
|
||
| /* | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,15 +2,18 @@ | |
| function getRandomNumber() { | ||
| return Math.random() * 10; | ||
| } | ||
| /* This function generates a random number between 0 and 10, including 0 but not including 10. However, the returned number includes decimal points and is not necessarily an integer. */ | ||
|
|
||
| // Add comments to explain what this function does. You're meant to use Google! | ||
| function combine2Words(word1, word2) { | ||
| return word1.concat(word2); | ||
| } | ||
| /* This function takes two strings, word1 and word2, as arguments and concatenates them together using the concat() method. The concat() method returns a new string that is the result of joining two or more strings. */ | ||
|
|
||
| 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. | ||
| return firstWord.concat(" ", secondWord.concat(" ", 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. this works, but could you think of a solution using Template Literals (I think it will be a cleaner solution)
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. is it: return |
||
| } | ||
|
|
||
| /* | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,10 @@ | |
| Sales tax is 20% of the price of the product. | ||
| */ | ||
|
|
||
| function calculateSalesTax() {} | ||
| function calculateSalesTax(price) { | ||
| price = price + price / 5; | ||
| return price; | ||
| } | ||
|
Comment on lines
+8
to
+11
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. See previous comment about pure functions |
||
|
|
||
| /* | ||
| CURRENCY FORMATTING | ||
|
|
@@ -17,7 +20,10 @@ function calculateSalesTax() {} | |
| Remember that the prices must include the sales tax (hint: you already wrote a function for this!) | ||
| */ | ||
|
|
||
| function addTaxAndFormatCurrency() {} | ||
| function addTaxAndFormatCurrency(price) { | ||
| price = price + price / 5; | ||
| return `£${price.toFixed(2)}`; | ||
| } | ||
|
Comment on lines
+23
to
+26
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. Another one on pure functions
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. Just found out what it is
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. yes! that would be a better approach. We should try avoiding modification of parameters because if you call the same function multiple times you will keep getting different answers. |
||
|
|
||
| /* | ||
| =================================================== | ||
|
|
||
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.
Please look into pure functions and argument mutation.
It's considered a bad practice for a function to have side effects. Can you think of which side effect this function has and why would it be bad practice?
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.
only thing came my mind is "5.7" and "0.99" must have been const