Skip to content
This repository was archived by the owner on Jan 14, 2024. 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
11 changes: 9 additions & 2 deletions extra/1-currency-conversion.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
Write a function that converts a price to USD (exchange rate is 1.4 $ to £)
*/

function convertToUSD() {}
function convertToUSD(price) {
return price * 1.4;
}



/*
CURRENCY CONVERSION
Expand All @@ -15,7 +19,10 @@ 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) {
price = price * 5.7 * 0.99;
return parseFloat(price.toFixed(2));
}
Comment on lines +22 to +25
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Please look into pure functions and argument mutation.

It's considered a bad practice for a function to have side effects. Can you think of which side effect this function has and why would it be bad practice?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

only thing came my mind is "5.7" and "0.99" must have been const


/* ======= TESTS - DO NOT MODIFY =====
There are some Tests in this file that will help you work out if your code is working.
Expand Down
19 changes: 10 additions & 9 deletions extra/2-piping.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,27 @@
the final result to the variable goodCode
*/

function add() {

function add(x, y) {
return x + y;
}

function multiply() {

function multiply(x, y) {
return x * y;
}

function format() {

function format(x) {
return `£${x}`;
}

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));

/* BETTER PRACTICE */

let goodCode =
const step1 = add(startingValue, 10);
const step2 = multiply(step1, 2);
const goodCode = format(step2);

/* ======= TESTS - DO NOT MODIFY =====
There are some Tests in this file that will help you work out if your code is working.
Expand Down
59 changes: 57 additions & 2 deletions extra/3-magic-8-ball.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,33 @@
// and return the answer.
function shakeBall() {
//Write your code in here
}

const possibleAnswers = [
"It is certain.",
"It is decidedly so.",
"Without a doubt.",
"Yes - definitely.",
"You may rely on it.",
"As I see it, yes.",
"Most likely.",
"Outlook good.",
"Yes.",
"Signs point to yes.",
"Reply hazy, try again.",
"Ask again later.",
"Better not tell you now.",
"Cannot predict now.",
"Concentrate and ask again.",
"Don't count on it.",
"My reply is no.",
"My sources say no.",
"Outlook not so good.",
"Very doubtful.",
];
const randomIndex = Math.floor(Math.random() * possibleAnswers.length);
console.log("The ball has shaken!");
return possibleAnswers[randomIndex];
}
/*
This function should say whether the answer it is given is
- very positive
Expand All @@ -60,6 +85,34 @@ function shakeBall() {
*/
function checkAnswer(answer) {
//Write your code in here

if (
answer == "It is certain." ||
answer == "It is decidedly so." ||
answer == "Without a doubt." ||
answer == "Yes - definitely." ||
answer == "You may rely on it."
) {
return "very positive";
} else if (
answer == "As I see it, yes." ||
answer == "Most likely." ||
answer == "Outlook good." ||
answer == "Yes." ||
answer == "Signs point to yes."
) {
return "positive";
} else if (
answer == "Reply hazy, try again." ||
answer == "Ask again later." ||
answer == "Better not tell you now." ||
answer == "Cannot predict now." ||
answer == "Concentrate and ask again."
) {
return "negative";
} else {
return "very negative";
}
Comment on lines +89 to +115
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This is fine, but you are using a lot of magic words here. Try to look into what magic words are and how this could be improved.

Hint:
You can either use a JS object or array index to determine whether it's positive or negative

}

/*
Expand Down Expand Up @@ -101,7 +154,9 @@ test("magic 8 ball returns different values each time", () => {
);
}

let seenPositivities = new Set(Array.from(seenAnswers.values()).map(checkAnswer));
let seenPositivities = new Set(
Array.from(seenAnswers.values()).map(checkAnswer)
);
if (seenPositivities.size < 2) {
throw Error(
"Expected to random answers with different positivities each time shakeBall was called, but always got the same one"
Expand Down
12 changes: 6 additions & 6 deletions mandatory/1-syntax-errors.js
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`;
}
function getTotal(a, b) {
total = a ++ b;
total = a + b;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This is missing a declaration, could you explain why it worked without a const or let declaration and why it would be a bad practice, even though it works?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

i made it global variable by mistake, but has a side effect it is it may cause unexpected behaviour


return "The total is total";
return "The total is " + total;
}

/*
Expand Down
7 changes: 3 additions & 4 deletions mandatory/2-logic-error.js
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;
}

/*
Expand Down
3 changes: 3 additions & 0 deletions mandatory/3-function-output.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@
function getRandomNumber() {
return Math.random() * 10;
}
/* This function generates a random number between 0 and 10, including 0 but not including 10. However, the returned number includes decimal points and is not necessarily an integer. */

// Add comments to explain what this function does. You're meant to use Google!
function combine2Words(word1, word2) {
return word1.concat(word2);
}
/* This function takes two strings, word1 and word2, as arguments and concatenates them together using the concat() method. The concat() method returns a new string that is the result of joining two or more strings. */

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.concat(" ", thirdWord));
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

this works, but could you think of a solution using Template Literals (I think it will be a cleaner solution)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

is it: return ${firstWord}${secondWord}${thirdWord}

}

/*
Expand Down
10 changes: 8 additions & 2 deletions mandatory/4-tax.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
Sales tax is 20% of the price of the product.
*/

function calculateSalesTax() {}
function calculateSalesTax(price) {
price = price + price / 5;
return price;
}
Comment on lines +8 to +11
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

See previous comment about pure functions


/*
CURRENCY FORMATTING
Expand All @@ -17,7 +20,10 @@ function calculateSalesTax() {}
Remember that the prices must include the sales tax (hint: you already wrote a function for this!)
*/

function addTaxAndFormatCurrency() {}
function addTaxAndFormatCurrency(price) {
price = price + price / 5;
return `£${price.toFixed(2)}`;
}
Comment on lines +23 to +26
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Another one on pure functions

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Just found out what it is
const newPrice = price + price/5

...a pure function should not modify its input parameters or any other external variables or state

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

yes! that would be a better approach. We should try avoiding modification of parameters because if you call the same function multiple times you will keep getting different answers.


/*
===================================================
Expand Down