Skip to content
This repository was archived by the owner on Oct 26, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions week-1/2-mandatory/1-syntax-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@

// 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 " + 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
let total = a + b

// Use string interpolation here
return "The total is %{total}"
return "The total is ${total}"
}

function getRemainder(a, b) {
return "The remainder is " + (a % b)
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand All @@ -31,6 +36,6 @@ function test(test_name, expr) {
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")
15 changes: 7 additions & 8 deletions week-1/2-mandatory/2-logic-error.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
// 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(word);
}

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}`)
}

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 multiply function", multiply(2, 3, 6) === 36)
22 changes: 20 additions & 2 deletions week-1/2-mandatory/3-function-output.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,39 @@
// Add comments to explain what this function does. You're meant to use Google!
/*
* getNumber() returns a random number within the range 0 < x < 10.
*
* The Math.random() function returns a floating-point number in the range 0 < x 1.
*
* Before the number is returned it is multiplied by ten thus extending the range.
*
*/
function getNumber() {
return Math.random() * 10;
}

// Add comments to explain what this function does. You're meant to use Google!
/*
* The s(w1, w2) function accepts two strings and returns a new string by concatenating
* the argumnents w1 and w2
* e.g if the function is call with the following arguments: s("Hello", " World!"), where the
* second argument is prepended with a space, then the function will return the following string:
* "Hello World!"
*/

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI - these could potentially be arrays also :)
[1, 2, 3].concat([4, 5, 6]) // returns [1, 2, 3, 4, 5, 6]

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} ${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
*/

Expand All @@ -41,4 +59,4 @@ test(
test(
"concatenate function - case 3 works",
concatenate("I", "am", 13) === "I am 13"
);
);
28 changes: 23 additions & 5 deletions week-1/2-mandatory/4-tax.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,41 @@
Sales tax is 20% of the price of the product
*/

function calculateSalesTax() {}
function calculateSalesTax(price) {
let salesTax = 0.2

return price * salesTax + price
}

/*
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 formatCurrency(price) {
/* The code below was my 1st attempt at solving this...
but I peeked into the abyss and found myNum.toFixed(2)...

let priceWithTax = calculateSalesTax(price).toString()

if (priceWithTax.indexOf(".") === -1)
return `£${priceWithTax}.00`
// check if the number of decimal places is less than two
if ((priceWithTax.length - 1) - priceWithTax.indexOf(".") === 1)
return `£ ${priceWithTax}0`

return `£${priceWithTax}`
*/

return `£${calculateSalesTax(price).toFixed(2)}`
}

/* ======= 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 4-tax.js` into your terminal
*/

Expand Down Expand Up @@ -51,4 +69,4 @@ test(
"formatCurrency function - case 2 works",
formatCurrency(17.5) === "£21.00"
);
test("formatCurrency function - case 3 works", formatCurrency(34) === "£40.80");
test("formatCurrency function - case 3 works", formatCurrency(34) === "£40.80");