Skip to content
This repository was archived by the owner on Jan 14, 2024. It is now read-only.
Closed
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 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 JS!")
console.log("This is the first exercise in the module.")
console.log("The module takes 3 weeks.")
4 changes: 3 additions & 1 deletion 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 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);
6 changes: 4 additions & 2 deletions exercises/E-strings-concatenation/exercise.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Start by creating a variable `message`

console.log(message);
var greetingStart = "Hi, my name is ";
var name = "Michael";
var greeting = greetingStart + name;
console.log(greeting);
4 changes: 3 additions & 1 deletion exercises/F-strings-methods/exercise.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Start by creating a variable `message`

var name = "Michael";
var nameLength = name.length;
var message = "My name is " + name + " and my name is " + nameLength +" characters long.";
console.log(message);
4 changes: 3 additions & 1 deletion exercises/F-strings-methods/exercise2.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const name = " Daniel ";

var trimmedName = name.trim();
var nameLength = name.length;
var message="My name is " + trimmedName + " and my name is " + nameLength + " characters long."
console.log(message);
7 changes: 7 additions & 0 deletions exercises/G-numbers/exercise.js
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
// Start by creating a variables `numberOfStudents` and `numberOfMentors`
var numberOfStudents = 15;
var numberOfMentors = 6;
var numberOfParticipants = numberOfStudents + numberOfMentors;
// Output
console.log("Number of students: " + numberOfStudents);
console.log("Number of mentors: " + numberOfMentors);
console.log("Total number of students and mentors: " + numberOfParticipants);
8 changes: 8 additions & 0 deletions exercises/I-floats/exercise.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
// Declare and initialise variables
var numberOfStudents = 15;
var numberOfMentors = 8;
var numberOfParticipants = numberOfStudents + numberOfMentors;
var percentageOfStudents = Math.round(numberOfStudents / numberOfParticipants * 100);
var percentageOfMentors = Math.round(numberOfMentors / numberOfParticipants * 100);
// Output
console.log("Percentage of students: " + percentageOfStudents + "%");
console.log("Percentage of mentors: " + percentageOfMentors + "%");

8 changes: 8 additions & 0 deletions exercises/J-functions/exercise.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
function halve(number) {
// complete the function here
return number / 2;
}

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

result = halve(23);
console.log(result);

result = halve(56);
console.log(result);


6 changes: 6 additions & 0 deletions exercises/J-functions/exercise2.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
function triple(number) {
// complete function here
return number * 3;
}

var result = triple(12);
console.log(result);

result = triple(23);
console.log(result);

result = triple(56);
console.log(result);
3 changes: 2 additions & 1 deletion 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(a,b) {
// Calculate the result of the function and return it
return a * b;
}

// Assign the result of calling the function the variable `result`
Expand Down
4 changes: 3 additions & 1 deletion exercises/K-functions-parameters/exercise2.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Declare your function first

function divide(a, b) {
return a / b;
}
var result = divide(3, 4);

console.log(result);
4 changes: 3 additions & 1 deletion exercises/K-functions-parameters/exercise3.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Write your function here

function createGreeting(name) {
return "Hello, my name is " + name;
}
var greeting = createGreeting("Daniel");

console.log(greeting);
6 changes: 4 additions & 2 deletions 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(a, b) {
return a + b;
}
// Call the function and assign to a variable `sum`

var sum = add(13, 124);
console.log(sum);
4 changes: 3 additions & 1 deletion exercises/K-functions-parameters/exercise5.js
Original file line number Diff line number Diff line change
@@ -1,5 +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);

console.log(greeting);
30 changes: 25 additions & 5 deletions exercises/L-functions-nested/exercise.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
var mentor1 = "Daniel";
var mentor2 = "Irina";
var mentor3 = "Mimi";
var mentor4 = "Rob";
var mentor5 = "Yohannes";
// Declare and initialise global variables
var numberOfStudents = 15;
var numberOfMentors = 8;
var numberOfParticipants = numberOfStudents + numberOfMentors;
var result; // percentage calculation result
var message; // message to be displayed

// This function calculates percentages
function calculatePercentage(a, b) {
return Math.round(a / b * 100); // approximate value
}

// Function to display the result of percentage calculation
function displayResult(val, desc) { // val = percentage value, desc = what the percentage is for
var message = "Percentage of ".concat(desc+": ", val + "%");
console.log(message);
}

// About students
result = calculatePercentage(numberOfStudents, numberOfParticipants);
message = displayResult(result, "students");

// About mentors
result = calculatePercentage(numberOfMentors, numberOfParticipants);
message = displayResult(result, "mentors");
23 changes: 23 additions & 0 deletions exercises/L-functions-nested/exercise2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
var mentor1 = "Daniel";
var mentor2 = "Irina";
var mentor3 = "Mimi";
var mentor4 = "Rob";
var mentor5 = "Yohannes";

// Function to change style of text to uppercase
function changeToUpperCase(text) {
return text.toUpperCase();
}

// Function to display greeting message, given name of a person to greet
function greetSomeone(name) {
var greeting = "Hello " + name;
var message = changeToUpperCase(greeting);
console.log(message);
}

greetSomeone(mentor1);
greetSomeone(mentor2);
greetSomeone(mentor3);
greetSomeone(mentor4);
greetSomeone(mentor5);
12 changes: 10 additions & 2 deletions extra/1-currency-conversion.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
Write a function that converts a price to USD (exchange rate is 1.4 $ to £)
*/

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

/*
CURRENCY FORMATTING
Expand All @@ -15,7 +17,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(price) {
const exchangeRate = 5.7;
const serviceCharge = 0.01 * price;
let actualPrice = price - serviceCharge;
let priceInBrl = actualPrice * exchangeRate;
return priceInBrl;
}

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

function add() {

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

function multiply() {

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

function format() {

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

const startingValue = 2

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

/* BETTER PRACTICE */

let goodCode =
let goodCode = add(10, startingValue);
goodCode = multiply(goodCode, 2);
goodCode = format(goodCode);

/* ======= TESTS - DO NOT MODIFY =====
There are some Tests in this file that will help you work out if your code is working.
Expand Down
66 changes: 66 additions & 0 deletions extra/3-magic-8-ball.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,34 @@
// and return the answer.
function shakeBall() {
//Write your code in here
console.log("The ball has shaken!");

let 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."
];
// Select a random answer from list
let index = Math.floor(Math.random() * answers.length);
let randomAnswer = answers[index];
return randomAnswer;
}

/*
Expand All @@ -60,6 +88,44 @@ function shakeBall() {
*/
function checkAnswer(answer) {
//Write your code in here
let answers = [
"very positive",
"positive",
"negative",
"very negative"
];

switch (answer) {
case "It is certain.":
case "It is decidedly so.":
case "Without a doubt.":
case "Yes - definitely.":
case "You may rely on it.":
answer = answers[0];
break;
case "As I see it, yes.":
case "Most likely.":
case "Outlook good.":
case "Yes.":
case "Signs point to yes.":
answer = answers[1];
break;
case "Reply hazy, try again.":
case "Ask again later.":
case "Better not tell you now.":
case "Cannot predict now.":
case "Concentrate and ask again.":
answer = answers[2];
break;
case "Don't count on it.":
case "My reply is no.":
case "My sources say no.":
case "Outlook not so good.":
case "Very doubtful.":
answer = answers[3];
break;
}
return answer;
}

/*
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 " + name "and I am " age + "years old";
function introduceMe(name, age) {
return "Hello, my name is " + name + " and I am " + age + " years old";
}

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

return "The total is total"
return "The total is " + total;
}

/*
Expand Down
10 changes: 5 additions & 5 deletions mandatory/2-logic-error.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
// 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;
function multiply(a, b, c) {
return a * b * c;
}

const { stringify } = require("querystring");
/*
===================================================
======= TESTS - DO NOT MODIFY BELOW THIS LINE =====
Expand Down
Loading