Skip to content
This repository has been archived by the owner on Oct 26, 2020. It is now read-only.

Week4 homework #929

Open
wants to merge 15 commits into
base: scotland-class4
Choose a base branch
from
9 changes: 6 additions & 3 deletions week-1/Homework/extra/1-currency-conversion.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@
Write a function that converts a price to USD (exchange rate is 1.4 $ to £)
*/

function convertToUSD() {}
function convertToUSD(pricePound) {
return pricePound * 1.4;
}

/*
CURRENCY FORMATTING
===================
The business is now breaking into the Brazilian market
Write a new function for converting to the Brazilian real (exchange rate is 5.7 BRL to £)
They have also decided that they should add a 1% fee to all foreign transactions
Find a way to add 1% to all currency conversions (think about the DRY principle)
*/

function convertToBRL() {}
function convertToBRL(pricePound) {
return (pricePound * 1.01) * 5.7;
}

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

Expand Down
19 changes: 13 additions & 6 deletions week-1/Homework/extra/2-piping.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,33 @@
the final result to the variable goodCode
*/

function add() {

}

function multiply() {
function add(a, b) {
const sum = Math.round((a + b) * 10) / 10;
return sum;
}

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

function format() {
function format(a) {
return "£" + a;

}

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 =
addValue = add(startingValue, 10);
multiplyValue = multiply(addValue, 2);

let goodCode = format(multiplyValue);

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

Expand Down
12 changes: 7 additions & 5 deletions week-1/Homework/mandatory/1-syntax-errors.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
// 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 getRemainder(a, b) {
remainder = a %% b;
remainder = a % b;


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

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
7 changes: 3 additions & 4 deletions week-1/Homework/mandatory/2-logic-error.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
// 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 ===== */
Expand Down
3 changes: 3 additions & 0 deletions week-1/Homework/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!
//Takes a random number between 0 (included) and 1 (excluded) and multiply it on 10
function getNumber() {
return Math.random() * 10;
}

// Add comments to explain what this function does. You're meant to use Google!
//concatenates the string arguments to the calling string and returns a new string.
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} ${secondWord} ${thirdWord}`
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
10 changes: 8 additions & 2 deletions week-1/Homework/mandatory/4-tax.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
Sales tax is 20% of the price of the product
*/

function calculateSalesTax() {}
function calculateSalesTax(amount) {
return amount * 1.2;
}

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

function formatCurrency() {}
function formatCurrency(amount) {
const price = calculateSalesTax(amount);
return "£" + price.toFixed(2);
}


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

Expand Down
35 changes: 33 additions & 2 deletions week-1/Homework/mandatory/5-magic-8-ball.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,20 @@ Very doubtful.

// This should log "The ball has shaken!"
// and return the answer.
function shakeBall() {}
function shakeBall(question) {
console.log("The ball has shaken!");
const array1 = ["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."];
let answer = array1[Math.floor(Math.random() * array1.length)];
return answer;
}



// The answer should come from shaking the ball
let answer;
Expand All @@ -55,7 +68,25 @@ let answer;
// - positive
// - negative
// - very negative
function checkAnswer() {}
function checkAnswer(answer) {
const arr1 = ["It is certain.", "It is decidedly so.",
"Without a doubt.", "Yes - definitely.", "You may rely on it."];
const arr2 = ["As I see it, yes.", "Most likely.", "Outlook good.", "Yes.",
"Signs point to yes."];
const arr3 = ["Reply hazy, try again.", "Ask again later.",
"Better not tell you now.", "Cannot predict now.", "Concentrate and ask again."];
const arr4 = ["Don't count on it.", "My reply is no.", "My sources say no.",
"Outlook not so good.", "Very doubtful."];
if (arr1.includes(answer)) {
return "very positive";
} else if (arr2.includes(answer)) {
return "positive";
} else if (arr3.includes(answer)) {
return "negative";
} else if (arr4.includes(answer)) {
return "very negative";
}
}

/* ======= TESTS - DO NOT MODIFY ===== */
const log = console.log;
Expand Down
10 changes: 5 additions & 5 deletions week-2/.archive/mandatory/1-fix-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Look at the tests and see how you can fix them.

function mood() {
let isHappy = true;
let isHappy = false;

if (isHappy) {
return "I am happy";
Expand All @@ -13,7 +13,7 @@ function mood() {

function greaterThan10() {
let num = 10;
let isBigEnough;
let isBigEnough = num >= 10;

if (isBigEnough) {
return "num is greater than or equal to 10";
Expand All @@ -24,21 +24,21 @@ function greaterThan10() {

function sortArray() {
let letters = ["a", "n", "c", "e", "z", "f"];
let sortedLetters;
let sortedLetters = letters.sort();

return sortedLetters;
}

function first5() {
let numbers = [1, 2, 3, 4, 5, 6, 7, 8];
let sliced;
let sliced = numbers.slice(0, 5);

return sliced;
}

function get3rdIndex(arr) {
let index = 3;
let element;
let element = arr[index];

return element;
}
Expand Down
33 changes: 25 additions & 8 deletions week-2/.archive/mandatory/2-function-creation.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ Write a function that:
- removes any forward slashes (/) in the strings
- makes the string all lowercase
*/
function tidyUpString(strArr) {}

function tidyUpString(strArr) {
return newTidyStr = strArr.map(function (el) {
return el.trim().replace("/", "").toLowerCase();
});
}

/*
Complete the function to check if the variable `num` satisfies the following requirements:
Expand All @@ -15,7 +20,12 @@ Complete the function to check if the variable `num` satisfies the following req
Tip: use logical operators
*/

function validate(num) {}
function validate(num) {
if (typeof num === "number" && num % 2 === 0 && num <= 100) {
return true;
}
return false;
}

/*
Write a function that removes an element from an array
Expand All @@ -26,8 +36,10 @@ The function must:
*/

function remove(arr, index) {
return; // complete this statement
arr.splice(index, 1);
return arr;
}
//return; // complete this statement

/*
Write a function that:
Expand All @@ -38,7 +50,12 @@ Write a function that:
*/

function formatPercentage(arr) {

for (let i = 0; i < arr.length; i++) {
arr[i] = arr[i].toPrecision(2);
if (arr[i] > 100) arr[i] = 100;
arr[i] = arr[i] + "%";
}
return arr;
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down Expand Up @@ -72,7 +89,7 @@ test(
"daniel",
"irina",
"gordon",
"ashleigh"
"ashleigh",
])
);
test(
Expand Down Expand Up @@ -101,7 +118,7 @@ test(
"c",
"d",
"e",
"f"
"f",
])
);

Expand All @@ -111,6 +128,6 @@ test(
"23%",
"18%",
"100%",
"0.37%"
"0.37%",
])
);
);
2 changes: 1 addition & 1 deletion week-2/.archive/mandatory/3-playing-computer.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const f2 = function(a, b) {

console.log(x);
console.log(a);
console.log(b);
//console.log(b);

for (let i = 0; i < 5; ++i) {
a = a + 1;
Expand Down
14 changes: 13 additions & 1 deletion week-2/.archive/mandatory/4-sorting-algorithm.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,19 @@ You don't have to worry about making this algorithm work fast! The idea is to ge
"think" like a computer and practice your knowledge of basic JavaScript.
*/

function sortAges(arr) {}
function sortAges(arr) {
var len = arr.length;
for (var i = len - 1; i >= 0; i--) {
for (var j = 1; j <= i; j++) {
if (arr[j - 1] > arr[j]) {
var temp = arr[j - 1];
arr[j - 1] = arr[j];
arr[j] = temp;
}
}
}
return arr;
}

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

Expand Down
10 changes: 9 additions & 1 deletion week-4/Homework/mandatory/1-food.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ console.log("Favorite food is " + myFavFood.name);
// and returns the address of where to buy it

function getTakeAwayAddress(food) {
return food.takeAway.address;
// write code here
}

Expand All @@ -26,22 +27,29 @@ console.log("I can buy my favorite food from " + getTakeAwayAddress(myFavFood));
// and returns true if it's vegetarian, or false if not

function isVeggie(food) {
return food.isVeggie;
// write code here
}

console.log("Is my favorite vegetarian? " + isVeggie(food));
console.log("Is my favorite vegetarian? " + isVeggie(myFavFood));

// 3. Declare a function "isLowCalorie" that accepts "food" as a parameter
// and returns true if it has less than 600 calories, or false otherwise
// write your own console.log() that calls the function to test it, as in the examples above!
// no example code is provided here!

function isLowCalorie(food) {
return food.caloriesPerPortion < 600;
};

console.log("Is the food low calorie? " + isLowCalorie(myFavFood));
//---------------------------------------

// 3. Declare a function "isSafeForNutAllergies" that accepts "food" as a parameter
// and returns false if it found the word "sesame" in the ingredients, or true otherwise

function isSafeForNutAllergies(food) {
return !food.ingredients.includes("sesame seeds");
//write code here

Choose a reason for hiding this comment

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

You need to check for the word "sesame" in the ingredients so it's a bit more than checking if the food.ingredients array includes sesame seeds. You need to use an array function that will iterate (loop) through the ingredients and then check whether each string contains that function. I would suggest that you will need to use an array function, a function of your own that you use on each element and then a function of string to check what each one includes. From this you should be able to work out whether it's true or not that there is at least one ingredient with the word sesame in it.

}

Expand Down
Loading