Skip to content
This repository was archived by the owner on Jan 14, 2024. It is now read-only.
Closed
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
12 changes: 6 additions & 6 deletions exercises/A-setup-ide/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ There are some tools that will help you to write code. One of these, [Prettier](

### 1. Install prettier

* In Visual Studio open the extensions panel (see https://code.visualstudio.com/docs/editor/extension-gallery#_browse-and-install-extensions)
* Search for `Prettier - Code formatter`
* Click install on the top result
- In Visual Studio open the extensions panel (see https://code.visualstudio.com/docs/editor/extension-gallery#_browse-and-install-extensions)
- Search for `Prettier - Code formatter`
- Click install on the top result

### 2. Enable formatting on save

* In Visual Studio open the settings file (see https://code.visualstudio.com/docs/getstarted/settings#_creating-user-and-workspace-settings)
* Search for `editor format`
* Set `editor.formatOnSave` and `editor.formatOnPaste` to true
- In Visual Studio open the settings file (see https://code.visualstudio.com/docs/getstarted/settings#_creating-user-and-workspace-settings)
- Search for `editor format`
- Set `editor.formatOnSave` and `editor.formatOnPaste` to true
12 changes: 10 additions & 2 deletions extra/1-currency-conversion.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
Write a function that converts a price to USD (exchange rate is 1.4 $ to £)
*/

function convertToUSD() {}
function convertToUSD(pricesInPounds) {
// converts prices in pounds to prices American Dollar
return pricesInPounds * 1.4;
}

/*
CURRENCY FORMATTING
Expand All @@ -15,7 +18,12 @@ 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(pricesInPounds) {
// converts prices in pound to prices in Brazilian Real
return 5.7 * pricesInPounds * 0.99;


}

/* ======= 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: 11 additions & 8 deletions extra/2-piping.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,29 @@
the final result to the variable goodCode
*/

function add() {

function add(firstNumber, secondNumber) {
return firstNumber + secondNumber;
}

function multiply() {

function multiply(firstNumber, secondNumber) {
return firstNumber * secondNumber;
}

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

}

const startingValue = 2

// Why can this code be seen as bad practice? Comment your answer.
let badCode =
// bad code because it is not readable
let badCode = format(multiply(2, add(startingValue, 10)));

/* BETTER PRACTICE */

let goodCode =
let addTen = add(startingValue,10) ;
let multiplyByTwo = multiply (2, addTen);
let goodCode = format(multiplyByTwo);

/* ======= TESTS - DO NOT MODIFY =====
There are some Tests in this file that will help you work out if your code is working.
Expand Down
88 changes: 42 additions & 46 deletions extra/3-magic-8-ball.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,39 @@
/**

Let's peer into the future using a Magic 8 Ball!
https://en.wikipedia.org/wiki/Magic_8-Ball

There are a few steps to being able view the future though:
* Ask a question
* Shake the ball
* Get an answer
* Decide if it's positive or negative

The question can be anything, but the answers are fixed,
and have different levels of positivity or negativity.

Below are the possible answers:

## Very positive
It is certain.
It is decidedly so.
Without a doubt.
Yes - definitely.
You may rely on it.

## Positive
As I see it, yes.
Most likely.
Outlook good.
Yes.
Signs point to yes.

## Negative
Reply hazy, try again.
Ask again later.
Better not tell you now.
Cannot predict now.
Concentrate and ask again.

## Very negative
Don't count on it.
My reply is no.
My sources say no.
Outlook not so good.
Very doubtful.
*/

// creating a magicAnswer object with all the possible answers
const magicAnswer = {
"very positive": ["It is certain.",
"It is decidedly so.",
"Without a doubt.",
"Yes - definitely.",
"You may rely on it."]
,
"positive": ["As I see it, yes." ,
"Most likely.",
" Outlook good.",
"Yes.",
"Signs point to yes."],
"negative": ["Reply hazy, try again.",
"Ask again later.",
"Better not tell you now.",
"Cannot predict now.",
"Concentrate and ask again."],
"very negative": [ "Don't count on it.",
"My reply is no.",
"My sources say no.",
"Outlook not so good.",
"Very doubtful."]
};
// This should log "The ball has shaken!"
// and return the answer.
function shakeBall() {
//Write your code in here
console.log("The ball has shaken!");
// outputs 0,1,2,3 corresponding to very/positive,very/negative
let answerCategory = Math.round(Math.random() * 3);
// these represent the arrays' five options i.e. 0,1,2,3,4
let possibleAnswer = Math.round(Math.random() * 4);
// the function returns random answers every time it is invoked
return magicAnswer[Object.keys(magicAnswer)[answerCategory]][possibleAnswer];

}

/*
Expand All @@ -59,7 +46,16 @@ 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
for (const group of Object.keys(magicAnswer))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Just a small note here. Please ensure that the indentation is correctly done (especially the brackets). It should be:

for(...) {
    //code here
}

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.

Prettier has been working fine . I will have a look. Thanks !

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

// iterating through all the keys of the magicAnswer object
{
if (magicAnswer[group].includes(answer))
// checking which category the answer belongs to
{
return group;
}
}

}

/*
Expand Down
12 changes: 7 additions & 5 deletions mandatory/1-syntax-errors.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
// 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 getTotal(a, b) {
total = a ++ b;
total = a + b;

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

/*
Expand Down
8 changes: 4 additions & 4 deletions mandatory/2-logic-error.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// 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 ;
}

/*
Expand Down
3 changes: 3 additions & 0 deletions mandatory/3-function-output.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
// Add comments to explain what this function does. You're meant to use Google!
function getNumber() {
// generates random floating numbers between zero and 10 but never 10
return Math.random() * 10;
}

// Add comments to explain what this function does. You're meant to use Google!
function s(w1, w2) {
// stringing together two strings-no spaces- and returns a string

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

A better way of commenting is: join two strings with no spaces

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.

Okay thanks

return w1.concat(w2);
}

function concatenate(firstWord, secondWord, thirdWord) {
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 perfectly well, but I would advise you to use one method of concatenating. In this line, you use both the plus sign + and the concat method. Since both methods are the same, it is always advised to use the plus sign as it is easier to read. Can you please change this line of code to reflect this?

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.

Sure will do thanks .

// 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.
}
Expand Down
11 changes: 9 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(prices) {
// the sales tax is 20% of the prices
return 0.2 * prices;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

It seems that the test has failed. Would you be able to fix this?

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.

Has it ? It passed on my machine . I will apply toFixed(2)

}

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

function addTaxAndFormatCurrency() {}
function addTaxAndFormatCurrency(prices) {
// this function adds on the 20% tax to the original price
let afterTax = prices + calculateSalesTax(prices);
return `£${afterTax}`
}
Comment on lines +25 to +27

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

you might need to use .toFixed(2) to force it to give two decimals

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.

Yeah that is a good idea. But , this passed the test too. I guess it is because we've multiplied it by 0.2 . I will look into it. Thanks !


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