-
-
Notifications
You must be signed in to change notification settings - Fork 42
Hiba's homework WK1 (Mandatory+Extra) #3
base: master
Are you sure you want to change the base?
Changes from all commits
15b095a
1cc64e2
28676e5
648bd21
f27e588
2de905b
00dccfe
a06cb3a
957da95
c47a317
6e5327c
a467f4e
17aa903
fc51949
1ff97dc
508f480
c404fcc
5a39a5a
4a208cc
9ef94de
34b3b02
4fb0afc
8ce37fd
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,36 +1,40 @@ | ||
| // Syntax errors fixed | ||
|
|
||
|
|
||
| // There are syntax errors in this code - can you fix it to pass the tests? | ||
|
|
||
| function addNumbers(a b c) { | ||
| return a + b + c; | ||
| function addNumbers(a, b, c) { | ||
| return a + b + c; | ||
| } | ||
|
|
||
| function introduceMe(name, age) | ||
| return "Hello, my name is " + name "and I am " age + "years old"; | ||
| function introduceMe(name, age) { | ||
| return "Hello, my name is " + name + " and I am " + age + " years old"; | ||
| } | ||
|
|
||
| function getAddition(a, b) { | ||
| total = a ++ b | ||
| total = a + b; | ||
|
|
||
| // Use string interpolation here | ||
| return "The total is %{total}" | ||
| return `The total is ${total}`; | ||
| } | ||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== */ | ||
| // | ||
| // To run these tests type `node 1-syntax-errors.js` into your terminal | ||
| function getRemainder(a, b) { | ||
| return "The remainder is " + (a % b); | ||
| } | ||
|
|
||
| function test(test_name, expr) { | ||
| let status; | ||
| if (expr) { | ||
| status = "PASSED" | ||
| } else { | ||
| status = "FAILED" | ||
| } | ||
|
|
||
| console.log(`${test_name}: ${status}`) | ||
| let status; | ||
| if (expr) { | ||
| status = "PASSED"; | ||
| } else { | ||
| status = "FAILED"; | ||
| } | ||
| console.log(`${test_name}: ${status}`); | ||
| } | ||
|
|
||
| test("fixed addNumbers function - case 1", addNumbers(3,4,6) === 13) | ||
| test("fixed introduceMe function", introduceMe("Sonjide",27) === "Hello, my name is Sonjide and I am 27 years old") | ||
| test("fixed getRemainder function", getRemainder(23,5) === "The remainder is 3") | ||
| test("fixed addNumbers function - case 1", addNumbers(3, 4, 6) === 13); | ||
| test( | ||
| "fixed introduceMe function", | ||
| introduceMe("Sonjide", 27) === | ||
| "Hello, my name is Sonjide and I am 27 years old" | ||
| ); | ||
| test( | ||
| "fixed getRemainder function", | ||
| getRemainder(23, 5) === "The remainder is 3" | ||
| ); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,35 +1,32 @@ | ||
| // The syntax for this function is valid but it has an error, find it and fix it. | ||
|
|
||
| function trimWord(word) { | ||
| return wordtrim(); | ||
| return word.trim(); | ||
| } | ||
|
|
||
| function getWordLength(word) { | ||
| return "word".length() | ||
| return word.length; | ||
| } | ||
|
|
||
| function multiply(a, b, c) { | ||
| a * b * c; | ||
| return; | ||
| return a * b * c; | ||
| } | ||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== | ||
| There are some Tests in this file that will help you work out if your code is working. | ||
|
|
||
| To run these tests type `node 2-logic-error` into your terminal | ||
| */ | ||
|
|
||
| function test(test_name, expr) { | ||
| let status; | ||
| if (expr) { | ||
| status = "PASSED" | ||
| status = "PASSED"; | ||
| } else { | ||
| status = "FAILED" | ||
| status = "FAILED"; | ||
| } | ||
|
|
||
| console.log(`${test_name}: ${status}`) | ||
| console.log(`${test_name}: ${status}`); | ||
| } | ||
|
|
||
| test("fixed trimWord function", trimWord(" CodeYourFuture ") === "CodeYourFuture") | ||
| test("fixed wordLength function", getWordLength("A wild sentence appeared!") === 25) | ||
| test("fixed multiply function", multiply(2,3,6) === 36) | ||
| test( | ||
| "fixed trimWord function", | ||
| trimWord(" CodeYourFuture ") === "CodeYourFuture" | ||
| ); | ||
| test( | ||
| "fixed wordLength function", | ||
| getWordLength("A wild sentence appeared!") === 25 | ||
| ); | ||
| test("fixed multiply function", multiply(2, 3, 6) === 36); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,23 @@ | ||
| // Add comments to explain what this function does. You're meant to use Google! | ||
| /****************************************** | ||
| * This function return a positive | ||
| * random number less than 9 including 0 | ||
| * ****************************************/ | ||
| function getNumber() { | ||
| return Math.random() * 10; | ||
| } | ||
|
|
||
| // Add comments to explain what this function does. You're meant to use Google! | ||
| /****************************************** | ||
| * This function takes two strings and | ||
| * join (concatenate) them together | ||
| * ****************************************/ | ||
| function s(w1, w2) { | ||
| return w1.concat(w2); | ||
| } | ||
|
|
||
| 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 to expect in 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. You could also use template strings to get the same result. |
||
| } | ||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== | ||
| There are some Tests in this file that will help you work out if your code is working. | ||
|
|
||
| To run these tests type `node 3-function-output` into your terminal | ||
| */ | ||
|
|
||
| function test(test_name, expr) { | ||
| let status; | ||
| if (expr) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,29 +1,18 @@ | ||
| /* | ||
| SALES TAX | ||
| ========= | ||
| A business requires a program that calculates how much sales tax to charge | ||
| Sales tax is 20% of the price of the product | ||
| */ | ||
|
|
||
| function calculateSalesTax() {} | ||
|
|
||
| /* | ||
| CURRENCY FORMATTING | ||
| =================== | ||
| The business has informed you that prices must have 2 decimal places | ||
| They must also start with the currency symbol | ||
| Write a function that transforms numbers into the format £0.00 | ||
|
|
||
| Remember that the prices must include the sales tax (hint: you already wrote a function for this!) | ||
| */ | ||
|
|
||
| function formatCurrency() {} | ||
| function sTax(Price) { | ||
|
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. Use lowercase (camelCase) for javascript variables, functions and params. |
||
| let salesTax = 0.2 * Price; | ||
| return salesTax; | ||
| } | ||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== | ||
| There are some Tests in this file that will help you work out if your code is working. | ||
| function calculateSalesTax(Price) { | ||
| let tax = sTax(Price); | ||
| return tax + Price; | ||
| } | ||
|
|
||
| To run these tests type `node 4-tax.js` into your terminal | ||
| */ | ||
| function formatCurrency(Price) { | ||
| let tax = sTax(Price); | ||
| lastPrice = Price + tax; | ||
| return "£" + lastPrice.toFixed(2); | ||
| } | ||
|
|
||
| function test(test_name, expr) { | ||
| let status; | ||
|
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 well done. A couple of notes FYI. Nothing you can do but - one of the tests is badly written in my opinion. I would keep actually keep the function as
With that in mind we can simplify this program for the reader. Again nothing you did wrong here - just some extra info |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,30 +12,36 @@ | |
| - multiply the result by 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. aha - I see they have a similar idea :D. |
||
| - format it | ||
|
|
||
| 3. Write a more readable version of what you wrote in step 2 under the BETTER PRACTICE comment. Assign | ||
| 3. Write a more readable version of what you wrote | ||
| in step 2 under the BETTER PRACTICE comment. Assign | ||
| the final result to the variable goodCode | ||
| */ | ||
|
|
||
| function add() { | ||
|
|
||
| function add(a,b) { | ||
| let c=a+b | ||
| return parseFloat(c.toFixed(1)) | ||
| } | ||
|
|
||
| function multiply() { | ||
|
|
||
| function multiply(a,b) { | ||
| return(a*b) | ||
| } | ||
|
|
||
| function format() { | ||
|
|
||
| function format(price) { | ||
| return("£"+price) | ||
| } | ||
|
|
||
| const startingValue = 2 | ||
|
|
||
| // Why can this code be seen as bad practice? Comment your answer. | ||
| let badCode = | ||
| // The reader will take long time to understand whats going on | ||
| let badCode = format(2*(add(startingValue,10))) | ||
|
|
||
| /* BETTER PRACTICE */ | ||
| let sum=add(startingValue,10) | ||
| let mul=multiply(sum,2) | ||
| let form=format(mul) | ||
|
|
||
| let goodCode = | ||
| let goodCode = form | ||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== | ||
| There are some Tests in this file that will help you work out if your code is working. | ||
|
|
||
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.
True - but these could also be arrays :)
[1, 2, 3].concat([6, 7, 8]) // returns [1, 2, 3, 6, 7, 8]