-
-
Notifications
You must be signed in to change notification settings - Fork 480
london-10/iryna-lypnyk/completed-mandatory&extra-exercises #522
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 |
|---|---|---|
| @@ -1,2 +1,4 @@ | ||
| /node_modules/ | ||
| /package-lock.json | ||
| .idea | ||
| .vscode |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,26 +16,29 @@ | |
| the final result to the variable goodCode | ||
| */ | ||
|
|
||
| function add() { | ||
|
|
||
| function add(a, b) { | ||
| return a + b; | ||
| } | ||
|
|
||
| function multiply() { | ||
|
|
||
| function multiply(a, b) { | ||
| return a * b; | ||
| } | ||
|
|
||
| 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)); | ||
| //This code can be seen as bad practice because multiple methods are used in one line which makes it hard to determine the results from each methods execution. It is confusing and hard to follow. | ||
|
|
||
|
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 explanation was: //Multiple methods are used in one line which makes it hard to determine the results from each methods execution. It is confusing and hard to follow. 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 answer Schboostie |
||
| /* BETTER PRACTICE */ | ||
|
|
||
| let goodCode = | ||
| let sum = add(startingValue, 10); | ||
| let doubledSum = multiply(sum, 2); | ||
| let goodCode = format(doubledSum); | ||
|
|
||
| /* ======= 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 |
|---|---|---|
|
|
@@ -45,9 +45,12 @@ | |
|
|
||
| // This should log "The ball has shaken!" | ||
| // and return the answer. | ||
| function shakeBall() { | ||
| //Write your code in here | ||
| } | ||
| const veryPositive = "very positive"; | ||
| const positive = "positive"; | ||
| const negative = "negative"; | ||
| const veryNegative = "very negative"; | ||
| const allResults = [veryPositive, positive, negative, veryNegative] | ||
|
|
||
|
|
||
| /* | ||
| This function should say whether the answer it is given is | ||
|
|
@@ -58,10 +61,62 @@ 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 | ||
|
|
||
| const veryPositiveAnswers = [ | ||
| "It is certain.", | ||
| "It is decidedly so.", | ||
| "Without a doubt.", | ||
| "Yes - definitely.", | ||
| "You may rely on it.", | ||
| ]; | ||
|
|
||
| const positiveAnswers = [ | ||
| "As I see it, yes.", | ||
| "Most likely.", | ||
| "Outlook good.", | ||
| "Yes.", | ||
| "Signs point to yes.", | ||
| ]; | ||
|
|
||
| const negativeAnswers = [ | ||
| "Reply hazy, try again.", | ||
| "Ask again later.", | ||
| "Better not tell you now.", | ||
| "Cannot predict now.", | ||
| "Concentrate and ask again.", | ||
| ]; | ||
|
|
||
| const veryNegativeAnswers = [ | ||
| "Don't count on it.", | ||
| "My reply is no.", | ||
| "My sources say no.", | ||
| "Outlook not so good.", | ||
| "Very doubtful.", | ||
| ]; | ||
|
|
||
| let allAnswers = [veryPositiveAnswers, positiveAnswers, negativeAnswers, veryNegativeAnswers]; | ||
|
|
||
| function findRandomIndex(max){ | ||
| return Math.floor(Math.random() * max); | ||
|
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. Great use of max here! |
||
| } | ||
|
|
||
| function shakeBall() { | ||
| console.log("The ball has shaken!"); | ||
| let num1 = findRandomIndex(4); | ||
| let num2 = findRandomIndex(5); | ||
| return allAnswers[num1][num2]; | ||
|
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. Why are you returning two indices here? 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. Ah, I think I see. Do you need to call findRandomIndex within the shakeBall function so that it changes the output each time? |
||
|
|
||
| } | ||
|
|
||
| function checkAnswer(answer) { | ||
|
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 am not sure what this is doing (am new to javascript). Could you please explain? Remember to focus on readability. Looks great though and it's awesome to see all the tests passing. |
||
| let index; | ||
| allAnswers.map((answerItem, i)=>{ | ||
| if(answerItem.includes(answer)){ | ||
| index = i; | ||
| } | ||
| }) | ||
| return allResults[index]; | ||
| } | ||
| /* | ||
| ================================== | ||
| ======= TESTS - DO NOT MODIFY ===== | ||
|
|
||
| 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`; | ||
|
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. Good use of back tick when adding variables to a string. |
||
| } | ||
|
|
||
| function getTotal(a, b) { | ||
| total = a ++ b; | ||
|
|
||
| return "The total is total"; | ||
| let total = a + b; | ||
| return `The total is ${total}`; | ||
|
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! |
||
| } | ||
|
|
||
| /* | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,15 @@ | ||
| // The syntax for these functions is valid but there are some errors, find them and fix them | ||
|
|
||
| function trimWord(word) { | ||
| return wordtrim(); | ||
| return word.trim(); | ||
| } | ||
|
|
||
| function getStringLength(word) { | ||
| return "word".length(); | ||
| return word.length; | ||
| } | ||
|
|
||
| function multiply(a, b, c) { | ||
| a * b * c; | ||
| return; | ||
| return a * b * c; | ||
| } | ||
|
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. All functions looking good here, Iryna! |
||
|
|
||
| /* | ||
|
|
||
| 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! | ||
| // Returns a random integer from 0 to 9 | ||
|
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. You could add further comments to explain how Math.random() returns a decimal value which is then scaled to a desired range. 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. Agreed. It'll be helpful for collaborators to know what the random operator does. Also, is 9 the highest output of this function? What if math.random selects 0.9, for example?
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. Oh, yes, you are right. Math.random() returns a decimal value, and it can be scaled more than just to 9. But if to speak just about my function - it returns form 0 to 9. Thank you, Shahid and Katie! |
||
| function getRandomNumber() { | ||
| return Math.random() * 10; | ||
| } | ||
|
|
||
| // Add comments to explain what this function does. You're meant to use Google! | ||
| //Concatenate strings,numbers, arrays: | ||
|
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 start but think we can add a little more detail here. What specifically is this function doing? Also, concat can be used to concatanate strings and arrays, but can it concatanate numbers? |
||
| function combine2Words(word1, word2) { | ||
| return word1.concat(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. | ||
|
|
||
| return firstWord.concat(' ', 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. Nice one! |
||
| } | ||
|
|
||
| /* | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,9 @@ | |
| Sales tax is 20% of the price of the product. | ||
| */ | ||
|
|
||
| function calculateSalesTax() {} | ||
| function calculateSalesTax(price) { | ||
| return price * 1.2; | ||
| } | ||
|
|
||
| /* | ||
| CURRENCY FORMATTING | ||
|
|
@@ -17,7 +19,11 @@ function calculateSalesTax() {} | |
| Remember that the prices must include the sales tax (hint: you already wrote a function for this!) | ||
| */ | ||
|
|
||
| function addTaxAndFormatCurrency() {} | ||
| function addTaxAndFormatCurrency(number) { | ||
| let tax = calculateSalesTax(number); | ||
|
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 really like the use of the tax variable to break down the stages of the function. 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. Me too! |
||
| let taxFormatted = tax.toFixed(2) | ||
| return `£${taxFormatted}` | ||
| } | ||
|
|
||
| /* | ||
| =================================================== | ||
|
|
||
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 string interpolation here!