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
Binary file added .DS_Store
Binary file not shown.
Binary file added Untitled/.DS_Store
Binary file not shown.
10 changes: 8 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(price) {
let total=price * 1.4;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I really like how clear you've made this .

return total;
}

/*
CURRENCY CONVERSION
Expand All @@ -15,7 +18,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) {
let total=parseFloat((((price/100)*99)*5.7).toFixed(2));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

i think this is doing quite a lot on one line. Adding an extra line with a variable could make this more clear.

return total;
}

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

function add() {

function add(a,b) {
let sum=a+b;
return sum;
}

function multiply() {
function multiply(a,b) {
let mult=a*b;
return mult;

}

function format() {

function format(num) {
return "£"+num;
}

const startingValue = 2;


// Why can this code be seen as bad practice? Comment your answer.
let badCode =
//const is local variabe and we can't change its value
const startingValue = 2;

let badCode =format(multiply(add(startingValue,10),2));
console.log(badCode);


/* BETTER PRACTICE */
//it is not easy to read and understand/

//creating variable with let;//

let goodCode =
let summ= add(startingValue,10);
let multiplyy=multiply(summ,2);
let goodCode=format(multiplyy);
/* we can understand this code better /

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

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
## 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.
Expand All @@ -43,53 +35,117 @@
Very doubtful.
*/


const veryPositive = [
"It is certain.",
"It is decidedly so.",
"Without a doubt.",
"Yes - definitely.",
"You may rely on it.",
];

const positive = [
"As I see it, yes.",
"Most likely.",
"Outlook good.",
"Yes.",
"Signs point to yes.",
];

const negative = [
"Reply hazy, try again.",
"Ask again later.",
"Better not tell you now.",
"Cannot predict now.",
"Concentrate and ask again.",
];

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

const answers = [
"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.",
];

// 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 finalAnswer = Math.floor(Math.random() * answers.length);
return answers[finalAnswer];
};

/*
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) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Good job! Nice and clean:)

//Write your code in here
}
if (veryPositive.includes(answer)) {
return "very positive";
}
else if(positive.includes(answer)) {
return "positive";
}
else if (negative.includes(answer)) {
return "negative";
}
else {
return "very negative";
}
};

/*
==================================
======= 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 3-magic-8-ball` into your terminal
(Reminder: You must have run `npm install` one time before this will work!)
==================================
*/
const { toBeOneOf } = require("jest-extended");

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

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",
]);
});

test("magic 8 ball returns different values each time", () => {
const seenAnswers = new Set();
for (let i = 0; i < 10; ++i) {
Expand All @@ -100,19 +156,16 @@ test("magic 8 ball returns different values each time", () => {
"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"
);
}
});

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I like that you have used string interpolation here, nice!

}
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 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;
multyple = a * b * c;
return multyple;
}

/*
Expand Down
5 changes: 4 additions & 1 deletion mandatory/3-function-output.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Add comments to explain what this function does. You're meant to use Google!
//The java.lang.Math.random() method returns a pseudorandom double type number greater than or equal to 0.0 and less than 1.0
function getRandomNumber() {
return Math.random() * 10;
}
Expand All @@ -7,10 +8,12 @@ function getRandomNumber() {
function combine2Words(word1, word2) {
return word1.concat(word2);
}
// The concat() method joins 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.
// Look at the test case below to understand what this function is expected to return.
return firstWord.concat(" ",secondWord," ",thirdWord);
}

/*
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,11 @@
Sales tax is 20% of the price of the product.
*/

function calculateSalesTax() {}
function calculateSalesTax(price) {
return price + (price/100)*20 ;


}

/*
CURRENCY FORMATTING
Expand All @@ -17,7 +21,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) {
total=calculateSalesTax(price).toFixed(2)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Good use of .toFixed()

return "£"+total;
}

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