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
18 changes: 16 additions & 2 deletions extra/1-currency-conversion.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@
Write a function that converts a price to USD (exchange rate is 1.4 $ to £)
*/

function convertToUSD() {}
let total = 0.00;
let gbp = 0.00;

let brl = 0.00;

function convertToUSD(gbp) {
let usd = gbp * 1.4;
return usd;
}

/*
CURRENCY CONVERSION
Expand All @@ -15,7 +23,13 @@ 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(gbp) {
brl = gbp * 0.99 * 5.7 ;
brl = parseFloat(brl.toFixed(2));
return brl;

}

/* ======= TESTS - DO NOT MODIFY =====
There are some Tests in this file that will help you work out if your code is working.
Expand Down
24 changes: 16 additions & 8 deletions extra/2-piping.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,35 @@
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
*/
let num1;
let num2;

function add() {

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

function multiply() {

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

function format() {

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

const startingValue = 2;

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


/* BETTER PRACTICE */

let goodCode =

let addition = add(10,startingValue);
let multiplication = multiply(2, addition);
let goodCode = "£" + multiplication;

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

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.
*/

// This should log "The ball has shaken!"
// and return the answer.
// Rahma _ Berhan


//declare 2D array to contain all messages each type in separated row

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

// declare random1 and random2 variables- and their values will be generated rondomly
// will use these two numbers to choose message from the array

let random1;
let random2;

// this function will print message first, then will generate two numbers , then return message
function shakeBall() {
//Write your code in here
console.log("The ball has shaken!");

random1 = Math.floor(Math.random() * arrayOfMessages.length);
random2 = Math.floor(Math.random() * arrayOfMessages.length);

return arrayOfMessages[random1][random2];

}


/*
This function should say whether the answer it is given is
- very positive
Expand All @@ -58,10 +55,53 @@ function shakeBall() {

This function should expect to be called with any value which was returned by the shakeBall function.
*/

// this function will check the array one by one to search for the message stored in answer
// when the message is found ,it will save the row number in variable rowNum
// then it will move to switch to store the message type in type variable and then return it

function checkAnswer(answer) {
//Write your code in here
let type = "";
let rowNum = 0;

//search for answer
for (let i = 0 ; i <= 3 ; i++)
{
for (let j= 0 ;j<=4 ; j++)
{
if (answer == arrayOfMessages[i][j])
{
rowNum = i;
}
}
}

//return message type depending on the rowNum
switch (rowNum)
{
case 0:
type = "very positive";
break;

case 1:
type = "positive";
break;

case 2:
type = "negative";
break;

case 3:
type = "very negative";
break;
}
return type;

}




/*
==================================
======= TESTS - DO NOT MODIFY =====
Expand Down
11 changes: 6 additions & 5 deletions mandatory/1-syntax-errors.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
// 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`;
{
return "Hello, my name is " + name + " and I am " + age + " years old";
}

function getTotal(a, b) {
total = a ++ b;

return "The total is total";
total = a + b;
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;

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,16 +1,19 @@
// Add comments to explain what this function does. You're meant to use Google!
//this function will generate a random number between 0 & 1 then multiply it by 10.
function getRandomNumber() {
return Math.random() * 10;
}

// Add comments to explain what this function does. You're meant to use Google!
//The concat() method joins two or more strings. So this function will join word1 & word2 in one sentence.
function combine2Words(word1, word2) {
return word1.concat(word2);
}

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

/*
Expand Down
13 changes: 11 additions & 2 deletions mandatory/4-tax.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@
A business requires a program that calculates how much the price of a product is including sales tax
Sales tax is 20% of the price of the product.
*/
let price ;
let total ;

function calculateSalesTax() {}
function calculateSalesTax(price) {
total = price + (0.20 * price);
return total;
}

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

function addTaxAndFormatCurrency() {}
function addTaxAndFormatCurrency(price) {
let tax = calculateSalesTax(price);
tax = parseFloat(tax).toFixed(2);
return `£${tax}`;
}

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