Skip to content
This repository was archived by the owner on Oct 26, 2020. 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
52 changes: 28 additions & 24 deletions week-1/2-mandatory/1-syntax-errors.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,40 @@
// Syntax errors fixed


// There are syntax errors in this code - can you fix it to pass the tests?

function addNumbers(a b c) {
return 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 getAddition(a, b) {
total = a ++ b
total = a + b;

// Use string interpolation here
return "The total is %{total}"
return `The total is ${total}`;
}

/* ======= TESTS - DO NOT MODIFY ===== */
//
// To run these tests type `node 1-syntax-errors.js` into your terminal
function getRemainder(a, b) {
return "The remainder is " + (a % b);
}

function test(test_name, expr) {
let status;
if (expr) {
status = "PASSED"
} else {
status = "FAILED"
}

console.log(`${test_name}: ${status}`)
let status;
if (expr) {
status = "PASSED";
} else {
status = "FAILED";
}
console.log(`${test_name}: ${status}`);
}

test("fixed addNumbers function - case 1", addNumbers(3,4,6) === 13)
test("fixed introduceMe function", introduceMe("Sonjide",27) === "Hello, my name is Sonjide and I am 27 years old")
test("fixed getRemainder function", getRemainder(23,5) === "The remainder is 3")
test("fixed addNumbers function - case 1", addNumbers(3, 4, 6) === 13);
test(
"fixed introduceMe function",
introduceMe("Sonjide", 27) ===
"Hello, my name is Sonjide and I am 27 years old"
);
test(
"fixed getRemainder function",
getRemainder(23, 5) === "The remainder is 3"
);
33 changes: 15 additions & 18 deletions week-1/2-mandatory/2-logic-error.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,32 @@
// 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;
}

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

To run these tests type `node 2-logic-error` into your terminal
*/

function test(test_name, expr) {
let status;
if (expr) {
status = "PASSED"
status = "PASSED";
} else {
status = "FAILED"
status = "FAILED";
}

console.log(`${test_name}: ${status}`)
console.log(`${test_name}: ${status}`);
}

test("fixed trimWord function", trimWord(" CodeYourFuture ") === "CodeYourFuture")
test("fixed wordLength function", getWordLength("A wild sentence appeared!") === 25)
test("fixed multiply function", multiply(2,3,6) === 36)
test(
"fixed trimWord function",
trimWord(" CodeYourFuture ") === "CodeYourFuture"
);
test(
"fixed wordLength function",
getWordLength("A wild sentence appeared!") === 25
);
test("fixed multiply function", multiply(2, 3, 6) === 36);
19 changes: 9 additions & 10 deletions week-1/2-mandatory/3-function-output.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
// Add comments to explain what this function does. You're meant to use Google!
/******************************************
* This function return a positive
* random number less than 9 including 0
* ****************************************/
function getNumber() {
return Math.random() * 10;
}

// Add comments to explain what this function does. You're meant to use Google!
/******************************************
* This function takes two strings and
* join (concatenate) them together
* ****************************************/

Choose a reason for hiding this comment

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

True - but these could also be arrays :)
[1, 2, 3].concat([6, 7, 8]) // returns [1, 2, 3, 6, 7, 8]

function s(w1, w2) {
return w1.concat(w2);
}

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 to expect in return
return firstWord + " ".concat(secondWord + " ", thirdWord);

Choose a reason for hiding this comment

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

Nice. You could also use template strings to get the same result.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

return `${firstWord} ${secondWord} ${thirdWord}`

}

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

To run these tests type `node 3-function-output` into your terminal
*/

function test(test_name, expr) {
let status;
if (expr) {
Expand Down
37 changes: 13 additions & 24 deletions week-1/2-mandatory/4-tax.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,18 @@
/*
SALES TAX
=========
A business requires a program that calculates how much sales tax to charge
Sales tax is 20% of the price of the product
*/

function calculateSalesTax() {}

/*
CURRENCY FORMATTING
===================
The business has informed you that prices must have 2 decimal places
They must also start with the currency symbol
Write a function that transforms numbers into the format £0.00

Remember that the prices must include the sales tax (hint: you already wrote a function for this!)
*/

function formatCurrency() {}
function sTax(Price) {

Choose a reason for hiding this comment

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

Use lowercase (camelCase) for javascript variables, functions and params.
We only use uppercase for Classes and Components. Also - always give functions readable names that explain what the function does. This is not always easy but try to take the time as it has huge benefits for the readability of your code.

let salesTax = 0.2 * Price;
return salesTax;
}

/* ======= TESTS - DO NOT MODIFY =====
There are some Tests in this file that will help you work out if your code is working.
function calculateSalesTax(Price) {
let tax = sTax(Price);
return tax + Price;
}

To run these tests type `node 4-tax.js` into your terminal
*/
function formatCurrency(Price) {
let tax = sTax(Price);
lastPrice = Price + tax;
return "£" + lastPrice.toFixed(2);
}

function test(test_name, expr) {
let status;

Choose a reason for hiding this comment

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

This is well done.

A couple of notes FYI.

Nothing you can do but - one of the tests is badly written in my opinion.
the test for formatCurrency demands that the "calculation of price" should also be included.
This would be better named getFullPriceAsFormattedCurrency as it does much more than simply "formatCurrency".

I would keep actually keep the function as formatCurrency which just accepts a value and returns the same value as a currency.
This allows us to reuse it again and again anytime we need to print a value as currency.
Then to print the fullPrice in currency you would run

formatCurrency( calculateFullPrice(15) )

With that in mind we can simplify this program for the reader.

function calculatePercentage(percent, value) {
  return (percent / 100) * value
}

function calculateSalesTax(price) {
  return calculatePercentage(20, price)
}

function calculateFullPrice(price) {
  return calculateSalesTax(price) + price
}

function formatCurrency(value) {
  return `£${value.toFixed(2)}`
}

Again nothing you did wrong here - just some extra info

Expand Down
11 changes: 9 additions & 2 deletions week-1/3-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) {
return(1.4*price)

}

/*
CURRENCY FORMATTING
Expand All @@ -16,7 +19,11 @@ function convertToUSD() {}
Find a way to add 1% to all currency conversions (think about the DRY principle)
*/

function convertToBRL() {}
function convertToBRL(price) {
let conv=5.7*price
let fee=0.057*price
return(conv+fee)
}

/* ======= 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: 15 additions & 9 deletions week-1/3-extra/2-piping.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,36 @@
- multiply the result by 2

Choose a reason for hiding this comment

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

aha - I see they have a similar idea :D.
Nice

- format it

3. Write a more readable version of what you wrote in step 2 under the BETTER PRACTICE comment. Assign
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(a,b) {
let c=a+b
return parseFloat(c.toFixed(1))
}

function multiply() {

function multiply(a,b) {
return(a*b)
}

function format() {

function format(price) {
return("£"+price)
}

const startingValue = 2

// Why can this code be seen as bad practice? Comment your answer.
let badCode =
// The reader will take long time to understand whats going on
let badCode = format(2*(add(startingValue,10)))

/* BETTER PRACTICE */
let sum=add(startingValue,10)
let mul=multiply(sum,2)
let form=format(mul)

let goodCode =
let goodCode = form

/* ======= TESTS - DO NOT MODIFY =====
There are some Tests in this file that will help you work out if your code is working.
Expand Down
64 changes: 60 additions & 4 deletions week-1/3-extra/3-magic-8-ball.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,76 @@ My sources say no.
Outlook not so good.
Very doubtful.
*/

var result;
// This should log "The ball has shaken!"
// and return the answer.
function shakeBall() {}
function shakeBall() {
console.log("The ball has shaken!");
let num = Math.floor(Math.random() * answer.length);
result = answer[num];
return result;
}

// The answer should come from shaking the ball
let answer;
let answer = ["very positive", "positive", "negative", "very negative"];

// When checking the answer, we should tell someone if the answer is
// - very positive
// - positive
// - negative
// - very negative
function checkAnswer() {}
function checkAnswer() {
switch (result) {
case "very positive": {
let vP = [
"It is certain",
"It is decidedly so",
"Without a doubt",
"Yes - definitely",
"You may rely on it",
];
let num = Math.floor(Math.random() * vP.length);
return vP[num];
}

case "positive": {
let p = [
"It is certain",
"It is decidedly so",
"Without a doubt",
" Yes - definitely",
"You may rely on it",
];
let num = Math.floor(Math.random() * p.length);
return p[num];
}
case "negative": {
let n = [
"Reply hazy",
"try again",
"Ask again later",
"Better not tell you now",
"Cannot predict now",
"Concentrate and ask again",
];
let num = Math.floor(Math.random() * n.length);
}

case "very negative": {
let vN = [
"Dont count on it",
"My reply is no",
"My sources say no",
"Outlook not so good",
"Very doubtful",
];
let num = Math.floor(Math.random() * vN.length);
}
}

let num = Math.floor(Math.random() * answer.length);
return answer[num];
}

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