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
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Coursework
n# Coursework

Like learning a musical instrument, programming requires daily practice.

Expand All @@ -10,7 +10,6 @@ If you think you need to do more practice with the basics, then you can find som

## Setting up your code editor


There are some tools that will help you to write code. One of these, [Prettier](https://prettier.io/), formats your code, making it easier for you and others to read.

### 1. Install prettier
Expand All @@ -25,10 +24,9 @@ There are some tools that will help you to write code. One of these, [Prettier](
- Search for `editor format`
- Set `editor.formatOnSave` and `editor.formatOnPaste` to true


## Running the code/tests

The files for the mandatory/extra exercises are intended to be run as jest tests.
The files for the mandatory/extra exercises are intended to be run as jest tests.

- Once you have cloned the repository, run `npm install` once in the terminal to install jest (and any necessary dependencies).
- To run the tests for all mandatory/extra exercises, run `npm test`
Expand Down
20 changes: 13 additions & 7 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(UKPrice) {
const USPrice = UKPrice * 1.4;
return USPrice;
}

/*
CURRENCY CONVERSION
Expand All @@ -15,27 +18,30 @@ 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(UKPrice) {
const BRLPrice = 0.99 * UKPrice * 5.7;
return Number(BRLPrice.toFixed(2));
}

/* ======= TESTS - DO NOT MODIFY =====
/* ======= TESTS - DO NOT MODIFY =====
There are some Tests in this file that will help you work out if your code is working.

To run the tests for just this one file, type `npm test -- --testPathPattern 1-currency-conversion` into your terminal
(Reminder: You must have run `npm install` one time before this will work!)
*/

test("convertToUSD function works for £32", () => {
test('convertToUSD function works for £32', () => {
expect(convertToUSD(32)).toEqual(44.8);
});

test("convertToUSD function works for £50", () => {
test('convertToUSD function works for £50', () => {
expect(convertToUSD(50)).toEqual(70);
});

test("convertToBRL function works for £30", () => {
test('convertToBRL function works for £30', () => {
expect(convertToBRL(30)).toEqual(169.29);
});

test("convertToBRL function works for £1.50", () => {
test('convertToBRL function works for £1.50', () => {
expect(convertToBRL(1.5)).toEqual(8.46);
});
50 changes: 28 additions & 22 deletions extra/2-piping.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,63 +11,69 @@
- add 10 to startingValue
- 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
the final result to the variable goodCode
*/

function add() {

function add(num1, num2) {
return num1 + num2;
}

function multiply() {

function multiply(num1, num2) {
return num1 * num2;
}

function format() {

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

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 =
let goodCodeFunc = (startingValue) => {
const added = add(startingValue, 10);
const multiplied = multiply(added, 2);
const result = format(multiplied);
return result;
};

/* ======= TESTS - DO NOT MODIFY =====
let goodCode = goodCodeFunc(startingValue);
/* ======= TESTS - DO NOT MODIFY =====
There are some Tests in this file that will help you work out if your code is working.

ipe(add, multiply, format)(numb);
To run the tests for just this one file, type `npm test -- --testPathPattern 2-piping` into your terminal
(Reminder: You must have run `npm install` one time before this will work!)
*/

test("add function - case 1 works", () => {
test('add function - case 1 works', () => {
expect(add(1, 3)).toEqual(4);
});

test("add function - case 2 works", () => {
test('add function - case 2 works', () => {
expect(add(2.4, 5)).toEqual(7.4);
});

test("multiply function works", () => {
test('multiply function works', () => {
expect(multiply(2, 3)).toEqual(6);
});

test("format function works for whole number", () => {
expect(format(16)).toEqual("£16");
test('format function works for whole number', () => {
expect(format(16)).toEqual('£16');
});

test("format function works for decimal number", () => {
expect(format(10.1)).toEqual("£10.1");
test('format function works for decimal number', () => {
expect(format(10.1)).toEqual('£10.1');
});

test("badCode variable correctly assigned", () => {
expect(badCode).toEqual("£24");
test('badCode variable correctly assigned', () => {
expect(badCode).toEqual('£24');
});

test("goodCode variable correctly assigned", () => {
expect(goodCode).toEqual("£24");
test('goodCode variable correctly assigned', () => {
expect(goodCode).toEqual('£24');
});
94 changes: 69 additions & 25 deletions extra/3-magic-8-ball.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**

Let's peer into the future using a Magic 8 Ball!
https://en.wikipedia.org/wiki/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
Expand Down Expand Up @@ -43,26 +43,75 @@
Very doubtful.
*/

// This should log "The ball has shaken!"
// and return the answer.
let veryPositiveAnswers = [
'It is certain.',
'It is decidedly so.',
'Without a doubt.',
'Yes - definitely.',
'You may rely on it.',
];

let positiveAnswers = [
'As I see it, yes.',
'Most likely.',
'Outlook good.',
'Yes.',
'Signs point to yes.',
];

let negativeAnswers = [
'Reply hazy, try again.',
'Ask again later.',
'Better not tell you now.',
'Cannot predict now.',
'Concentrate and ask again.',
];

let veryNegativeAnswers = [
"Don't count on it.",
'My reply is no.',
'My sources say no.',
'Outlook not so good.',
'Very doubtful.',
];

let allAnswers = [
...veryPositiveAnswers,
...positiveAnswers,
...negativeAnswers,
...veryNegativeAnswers,
];

// This should log "The ball has shaken!"
// and return the answer.
function shakeBall() {
//Write your code in here
console.log('The ball has shaken!');
let randomIndex = Math.floor(Math.random() * allAnswers.length);
return allAnswers[randomIndex];
}

/*
/*
This function should say whether the answer it is given is
- very positive
- positive
- negative
- very negative

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
if (veryPositiveAnswers.includes(answer)) {
return 'very positive';
} else if (positiveAnswers.includes(answer)) {
return 'positive';
} else if (negativeAnswers.includes(answer)) {
return 'negative';
} else if (veryNegativeAnswers.includes(answer)) {
return 'very negative';
}
}

/*
/*
==================================
======= TESTS - DO NOT MODIFY =====

Expand All @@ -73,46 +122,41 @@ To run the tests for just this one file, type `npm test -- --testPathPattern 3-m
==================================
*/

test("whole magic 8 ball sequence", () => {
const consoleLogSpy = jest.spyOn(global.console, "log");
test('whole magic 8 ball sequence', () => {
const consoleLogSpy = jest.spyOn(global.console, 'log');
const answer = shakeBall();

expect(typeof answer).toEqual("string");
expect(typeof answer).toEqual('string');

expect(consoleLogSpy).toHaveBeenCalledTimes(1);
expect(consoleLogSpy).toHaveBeenLastCalledWith("The ball has shaken!");

expect(checkAnswer(answer)).toBeOneOf([
"very positive",
"positive",
"negative",
"very negative",
]);
expect(consoleLogSpy).toHaveBeenLastCalledWith('The ball has shaken!');

expect(checkAnswer(answer)).toBeOneOf(['very positive', 'positive', 'negative', 'very negative']);
});

test("magic 8 ball returns different values each time", () => {
test('magic 8 ball returns different values each time', () => {
const seenAnswers = new Set();
for (let i = 0; i < 10; ++i) {
seenAnswers.add(shakeBall());
}
if (seenAnswers.size < 2) {
throw Error(
"Expected to get different random answers each time shakeBall was called, but always got the same one"
'Expected to get different random answers each time shakeBall was called, but always got the same one'
);
}

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"
'Expected to random answers with different positivities each time shakeBall was called, but always got the same one'
);
}
});

test("checkAnswer works for `It is decidedly so.`", () => {
expect(checkAnswer("It is decidedly so.")).toEqual("very positive");
test('checkAnswer works for `It is decidedly so.`', () => {
expect(checkAnswer('It is decidedly so.')).toEqual('very positive');
});

test("checkAnswer works for `My reply is no.`", () => {
expect(checkAnswer("My reply is no.")).toEqual("very negative");
test('checkAnswer works for `My reply is no.`', () => {
expect(checkAnswer('My reply is no.')).toEqual('very negative');
});
25 changes: 12 additions & 13 deletions mandatory/1-syntax-errors.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
// 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;

return "The total is total";
return `The total is ${total}`;
}

/*
/*
===================================================
======= TESTS - DO NOT MODIFY BELOW THIS LINE =====

Expand All @@ -25,16 +26,14 @@ To run the tests for just this one file, type `npm test -- --testPathPattern 1-s
===================================================
*/

test("addNumbers adds numbers correctly", () => {
test('addNumbers adds numbers correctly', () => {
expect(addNumbers(3, 4, 6)).toEqual(13);
});

test("introduceMe function returns the correct string", () => {
expect(introduceMe("Sonjide", 27)).toEqual(
"Hello, my name is Sonjide and I am 27 years old"
);
test('introduceMe function returns the correct string', () => {
expect(introduceMe('Sonjide', 27)).toEqual('Hello, my name is Sonjide and I am 27 years old');
});

test("getTotal returns a string describing the total", () => {
expect(getTotal(23, 5)).toEqual("The total is 28");
test('getTotal returns a string describing the total', () => {
expect(getTotal(23, 5)).toEqual('The total is 28');
});
Loading