diff --git a/week-1/2-mandatory/1-syntax-errors.js b/week-1/2-mandatory/1-syntax-errors.js index 6910f28..b9a44e5 100644 --- a/week-1/2-mandatory/1-syntax-errors.js +++ b/week-1/2-mandatory/1-syntax-errors.js @@ -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" +); diff --git a/week-1/2-mandatory/2-logic-error.js b/week-1/2-mandatory/2-logic-error.js index 1e0a9d4..ca76532 100644 --- a/week-1/2-mandatory/2-logic-error.js +++ b/week-1/2-mandatory/2-logic-error.js @@ -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) \ No newline at end of file +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); diff --git a/week-1/2-mandatory/3-function-output.js b/week-1/2-mandatory/3-function-output.js index bbb88a2..03c1fcf 100644 --- a/week-1/2-mandatory/3-function-output.js +++ b/week-1/2-mandatory/3-function-output.js @@ -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); } -/* ======= 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) { diff --git a/week-1/2-mandatory/4-tax.js b/week-1/2-mandatory/4-tax.js index 6b84208..b90f379 100644 --- a/week-1/2-mandatory/4-tax.js +++ b/week-1/2-mandatory/4-tax.js @@ -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) { + 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; diff --git a/week-1/3-extra/1-currency-conversion.js b/week-1/3-extra/1-currency-conversion.js index 7f321d9..f5dd7b7 100644 --- a/week-1/3-extra/1-currency-conversion.js +++ b/week-1/3-extra/1-currency-conversion.js @@ -5,7 +5,10 @@ Write a function that converts a price to USD (exchange rate is 1.4 $ to £) */ -function convertToUSD() {} +function convertToUSD(price) { + return(1.4*price) + +} /* CURRENCY FORMATTING @@ -16,7 +19,11 @@ function convertToUSD() {} Find a way to add 1% to all currency conversions (think about the DRY principle) */ -function convertToBRL() {} +function convertToBRL(price) { + let conv=5.7*price + let fee=0.057*price + return(conv+fee) +} /* ======= TESTS - DO NOT MODIFY ===== There are some Tests in this file that will help you work out if your code is working. diff --git a/week-1/3-extra/2-piping.js b/week-1/3-extra/2-piping.js index 93c0bf7..9179823 100644 --- a/week-1/3-extra/2-piping.js +++ b/week-1/3-extra/2-piping.js @@ -12,30 +12,36 @@ - multiply the result by 2 - 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. diff --git a/week-1/3-extra/3-magic-8-ball.js b/week-1/3-extra/3-magic-8-ball.js index 1bb1089..6865804 100644 --- a/week-1/3-extra/3-magic-8-ball.js +++ b/week-1/3-extra/3-magic-8-ball.js @@ -42,20 +42,76 @@ My sources say no. Outlook not so good. Very doubtful. */ - +var result; // This should log "The ball has shaken!" // and return the answer. -function shakeBall() {} +function shakeBall() { + console.log("The ball has shaken!"); + let num = Math.floor(Math.random() * answer.length); + result = answer[num]; + return result; +} // The answer should come from shaking the ball -let answer; +let answer = ["very positive", "positive", "negative", "very negative"]; // When checking the answer, we should tell someone if the answer is // - very positive // - positive // - negative // - very negative -function checkAnswer() {} +function checkAnswer() { + switch (result) { + case "very positive": { + let vP = [ + "It is certain", + "It is decidedly so", + "Without a doubt", + "Yes - definitely", + "You may rely on it", + ]; + let num = Math.floor(Math.random() * vP.length); + return vP[num]; + } + + case "positive": { + let p = [ + "It is certain", + "It is decidedly so", + "Without a doubt", + " Yes - definitely", + "You may rely on it", + ]; + let num = Math.floor(Math.random() * p.length); + return p[num]; + } + case "negative": { + let n = [ + "Reply hazy", + "try again", + "Ask again later", + "Better not tell you now", + "Cannot predict now", + "Concentrate and ask again", + ]; + let num = Math.floor(Math.random() * n.length); + } + + case "very negative": { + let vN = [ + "Dont count on it", + "My reply is no", + "My sources say no", + "Outlook not so good", + "Very doubtful", + ]; + let num = Math.floor(Math.random() * vN.length); + } + } + + let num = Math.floor(Math.random() * answer.length); + return answer[num]; +} /* ======= TESTS - DO NOT MODIFY ===== There are some Tests in this file that will help you work out if your code is working.