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

Week3/patrick #83

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
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
3 changes: 3 additions & 0 deletions week-1/1-exercises/B-hello-world/exercise.js
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
console.log("Hello world");
console.log("Hello World. I just started learning JavaScript!");
console.log("Hello again World"); // error when quotation marks are absent
console.log(100);
4 changes: 3 additions & 1 deletion week-1/1-exercises/C-variables/exercise.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Start by creating a variable `greeting`

var greeting ="Hello world"
console.log(greeting);
console.log(greeting);
console.log(greeting);
4 changes: 3 additions & 1 deletion week-1/1-exercises/D-strings/exercise.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Start by creating a variable `message`

var message ="This is a string";
var messageType = typeof message;
console.log(message);
console.log(messageType);
12 changes: 11 additions & 1 deletion week-1/1-exercises/E-strings-concatenation/exercise.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
// Start by creating a variable `message`
var greetingStart = "Hello, my name is ";
var name = "Daniel";

console.log(message);
var greeting = greetingStart + name;
console.log(greeting);

name = "Patrick";
console.log(greeting); // BUG: will output Daniel

name = "Patrick";
var greeting = greetingStart + name;
console.log(greeting);
21 changes: 20 additions & 1 deletion week-1/1-exercises/F-strings-methods/exercise.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
// Start by creating a variable `message`
var name = "Daniel";
var nameLength = name.length;
console.log(nameLength);

console.log(message);
var nameLowerCase = name.toLowerCase();
console.log(nameLowerCase);

name = "Patrick";
nameLength = name.length; // have to repeat this variable so you don't get the nameLength of Daniel
console.log(nameLength);

var msg = `My name is ${name} and my name is ${nameLength} long.`;
console.log(msg);

var nameTrim = name.trim();
name = " Patrick . . . ";
msg = `My name is ${name} and my name is ${nameLength} long.`;
console.log(msg);

msg = `My name is ${nameTrim} and my name is ${nameLength} long.`;
console.log(msg);
11 changes: 11 additions & 0 deletions week-1/1-exercises/F-strings-methods/exercise2.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
const name = " Daniel ";
var nameLength = name.length;

var nameTrim = name.trim();

var myName = " Patrick ";
nameLength = myName.length;
var message = `My name is ${myName} and my name is ${nameLength} long.`;
console.log(message);

nameTrim = myName.trim();
nameLength = nameTrim.length;
message = `My name is ${nameTrim} and my name is ${nameLength} long.`;
console.log(message);
12 changes: 12 additions & 0 deletions week-1/1-exercises/G-numbers/exercise.js
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
// Start by creating a variables `numberOfStudents` and `numberOfMentors`
var age = 30;

var sum = 10 + 2;
var product = 10 * 2;
var quotient = 10 / 2;
var difference = 10 - 2;

var numberOfStudents = 15;
var numberOfMentors = 8;
var total = numberOfStudents + numberOfMentors;
var msg = `The total number of students and mentors: ${total}.`
console.log(msg);
9 changes: 9 additions & 0 deletions week-1/1-exercises/I-floats/exercise.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
var numberOfStudents = 15;
var numberOfMentors = 8;
var total = numberOfStudents + numberOfMentors;

var studentPercent = Math.round(numberOfStudents / total * 100);
var studentMsg = `Percentage students: ${studentPercent}%`;
console.log(studentMsg);

var mentorPercent = Math.round(numberOfMentors / total * 100);
var mentorMsg = `Percentage mentors: ${mentorPercent}%`;
console.log(mentorMsg);
7 changes: 7 additions & 0 deletions week-1/1-exercises/J-functions/exercise.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
function halve(number) {
// complete the function here
return number / 2;
}

var result = halve(12);

console.log(result);

var result = halve(100);
console.log(result);

var result = halve(15);
console.log(result);
1 change: 1 addition & 0 deletions week-1/1-exercises/J-functions/exercise2.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
function triple(number) {
// complete function here
return number * 3;
}

var result = triple(12);
Expand Down
3 changes: 2 additions & 1 deletion week-1/1-exercises/K-functions-parameters/exercise.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Complete the function so that it takes input parameters
function multiply() {
function multiply(num1, num2) {
// Calculate the result of the function and return it
return num1 * num2;
}

// Assign the result of calling the function the variable `result`
Expand Down
3 changes: 3 additions & 0 deletions week-1/1-exercises/K-functions-parameters/exercise2.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// Declare your function first
function divide(num1, num2) {
return num1 / num2;
}

var result = divide(3, 4);

Expand Down
3 changes: 3 additions & 0 deletions week-1/1-exercises/K-functions-parameters/exercise3.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// Write your function here
function createGreeting(name) {
return `Hello, my name is ${name}`;
}

var greeting = createGreeting("Daniel");

Expand Down
6 changes: 4 additions & 2 deletions week-1/1-exercises/K-functions-parameters/exercise4.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Declare your function first

function add(num1, num2) {
return num1 + num2;
}
// Call the function and assign to a variable `sum`

var sum = add(13, 124);
console.log(sum);
3 changes: 3 additions & 0 deletions week-1/1-exercises/K-functions-parameters/exercise5.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// Declare your function here
function createLongGreeting(name, age) {
return `Hello, my name is ${name} and I'm ${age} years old`;
}

const greeting = createLongGreeting("Daniel", 30);

Expand Down
73 changes: 73 additions & 0 deletions week-1/1-exercises/L-functions-nested/exercise.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,78 @@
// Exercise 1
function studentPercent(numberOfStudents, numberOfMentors) {
var total = numberOfStudents + numberOfMentors;
var studentPercent = Math.round(numberOfStudents / total * 100);
return studentPercent;
}

function mentorPercent(numberOfStudents, numberOfMentors) {
var total = numberOfStudents + numberOfMentors;
var mentorPercent = Math.round(numberOfMentors / total * 100);
return mentorPercent;
}

// console.log(studentPercent(15, 8)); -> this line tests function studentPercent
// console.log(mentorPercent(15, 8)); -> this line tests function mentorPercent

function message(numberOfStudents, numberOfMentors) {
var msgStudentPercent = `Percentage students: ${studentPercent(numberOfStudents, numberOfMentors)}%`
var msgMentorPercent = `Percentage mentors: ${mentorPercent(numberOfStudents, numberOfMentors)}%`
return msgStudentPercent + "\n" + msgMentorPercent;
}
console.log(message(15, 8));

// Exercise 2
var mentor1 = "Daniel";
var mentor2 = "Irina";
var mentor3 = "Mimi";
var mentor4 = "Rob";
var mentor5 = "Yohannes";

var shout = mentor1.toUpperCase();
var greeting = `HELLO ${shout}`;
console.log(greeting);
var shout = mentor2.toUpperCase();
var greeting = `HELLO ${shout}`;
console.log(greeting);
var shout = mentor3.toUpperCase();
var greeting = `HELLO ${shout}`;
console.log(greeting);
var shout = mentor4.toUpperCase();
var greeting = `HELLO ${shout}`;
console.log(greeting);
var shout = mentor5.toUpperCase();
var greeting = `HELLO ${shout}`;
console.log(greeting);

var mentors = "Daniel, Irina, Mimi, Rob, Yohannes"
console.log(mentors);

var greetings = "HELLO ";
var mentorsArray = [mentor1, mentor2, mentor3, mentor4, mentor5];
console.log(greetings+mentorsArray[0], greetings+mentorsArray[1], greetings+mentorsArray[2], greetings+mentorsArray[3], greetings+mentorsArray[4]);

function mentors() {
for (i = 0;i < mentorsArray.length; i++) {
// var SHOUT = greetings + mentorsArray[i]; *** doesn't work ***
// return SHOUT; *** doesn't work, so may as well stick to --> return mentorsArray[i]; ***
return mentorsArray[i];
}
}
console.log(mentors);



// function mentors() {
// for (var i=0; i < mentorsArray.length; i++) {
// return mentors;
// }
// }
// console.log(mentors());


// function greeting(name) {
// var mentor1 = "Daniel";
// var name = mentor1.toUpperCase();
// return `HELLO ${mentor1}`;
// }
// console.log(greeting(daniel));
16 changes: 11 additions & 5 deletions week-1/2-mandatory/1-syntax-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@

// 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 getAddition(a, b) {
total = a ++ b
var total = a + b;

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

function getRemainder(a, b) {
var remainder = a % b;
return `The remainder is ${remainder}`;
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
7 changes: 3 additions & 4 deletions week-1/2-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
5 changes: 5 additions & 0 deletions week-1/2-mandatory/3-function-output.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
// Add comments to explain what this function does. You're meant to use Google!
// Math.random returns a random number lower than 1 and then times it by 10. So the output is always < 10.
function getNumber() {
return Math.random() * 10;
}

// Add comments to explain what this function does. You're meant to use Google!
// This function joins 2 parameter values. So the output is w1, w2. It can be used to create a new set of array.
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
var space = " " + secondWord + " ";
return firstWord.concat(space, thirdWord);
}
console.log(concatenate('I', 'love', 'pizza'));

/* ======= TESTS - DO NOT MODIFY =====
There are some Tests in this file that will help you work out if your code is working.
Expand Down
40 changes: 38 additions & 2 deletions week-1/2-mandatory/4-tax.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,19 @@
Sales tax is 20% of the price of the product
*/

function calculateSalesTax() {}
function calculateSalesTax(sales) {
var tax = .2;
var salesTax = sales * tax;
var twoDecimals = salesTax.toFixed(2); // to add 2 decimal places
var twoDecimalsNum = parseFloat(twoDecimals); // converts text to number
return sales + twoDecimalsNum;

// var salesaddedTax = sales * 1.2;
// var twoDecimals = salesaddedTax.toFixed(2);
// return twoDecimals;
}
console.log(calculateSalesTax(17.5));
console.log(calculateSalesTax(34));

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

function formatCurrency() {}
function formatCurrency(sales) {
function calculateSalesTax(sales) {
var tax = .2;
var salesTax = sales * tax;
var twoDecimals = salesTax.toFixed(2); // to add 2 decimal places
var twoDecimalsNum = parseFloat(twoDecimals); // converts text to number
return sales + twoDecimalsNum;
}
var number = calculateSalesTax(sales);
console.log(new Intl.NumberFormat('en-GB', {
style: 'currency',
currency: 'GBP'
})
.format(number)
);
}

// var number = calculateSalesTax(34);
// console.log(new Intl.NumberFormat('en-GB', {
// style: 'currency',
// currency: 'GBP'
// })
// .format(number)
// );


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