-
-
Notifications
You must be signed in to change notification settings - Fork 476
london-10/iryna-lypnyk/completed-mandatory&extra-exercises #522
Changes from 1 commit
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 |
|---|---|---|
|
|
@@ -5,7 +5,9 @@ | |
| Write a function that converts a price to USD (exchange rate is 1.4 $ to £) | ||
| */ | ||
|
|
||
| function convertToUSD() {} | ||
| function convertToUSD(price) { | ||
| return Number((price * 1.4).toFixed(2)); | ||
| } | ||
|
|
||
| /* | ||
| CURRENCY CONVERSION | ||
|
|
@@ -15,7 +17,9 @@ function convertToUSD() {} | |
| They have also decided that they should add a 1% fee to all foreign transactions, which means you only convert 99% of the £ to BRL. | ||
| */ | ||
|
|
||
| function convertToBRL() {} | ||
| function convertToBRL(price) { | ||
| return Number(((price * 5.7)*0.99).toFixed(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. This is smart and gets the job done, but it is trying to achieve too much on one line - making it vulnerable to bugs and difficult to read. How can you take these steps and break them into separate lines? |
||
| } | ||
|
|
||
| /* ======= 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 |
|---|---|---|
|
|
@@ -16,26 +16,27 @@ | |
| 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}` | ||
|
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 string interpolation here! |
||
| } | ||
|
|
||
| const startingValue = 2; | ||
|
|
||
| // Why can this code be seen as bad practice? Comment your answer. | ||
| let badCode = | ||
| let badCode = `£${(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. I can not see a comment to explain why this is bad code. Did you add one? 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 could use a little work, as the exercise above asks you to use the functions (like you have below in goodCode). Why would all your functions on one line (like you have in goodCode) be bad? Hint - bugs, readability
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 concentrated so much on JavaScript that I didn`t notice task "Comment your answer..." ))) I updated my code and pushed it |
||
|
|
||
|
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 goodCode = 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. This does the job, but good code is highly readable and easy to modify so tends to be broken out into multiple lines. Can you break this out into steps? |
||
|
|
||
| /* ======= 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,61 @@ 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 + price/5; | ||
|
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. It can be useful to declare separate variables at stages of each operation. When calculations become more complexed it may be necessary to check various stages to see if and when mistakes occur. |
||
| } | ||
|
|
||
| /* | ||
| CURRENCY FORMATTING | ||
|
|
@@ -17,7 +19,10 @@ 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! |
||
| return `£${(tax).toFixed(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. Excellent |
||
| } | ||
|
|
||
| /* | ||
| =================================================== | ||
|
|
||
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!