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
1 change: 1 addition & 0 deletions exercises/B-hello-world/exercise.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
console.log("Hello world");
console.log("Hello world2");
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`

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

console.log(message);
let message = "abcdef";
console.log(typeof message);
4 changes: 3 additions & 1 deletion 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`

let hello = "Hello, ";
let secondPart = "my name is Daniel"
let message = hello + secondPart;
console.log(message);
12 changes: 11 additions & 1 deletion exercises/F-strings-methods/exercise.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
// Start by creating a variable `message`

let message = "My name is Patryk";
let name = "Patryk";
console.log(message);
console.log(name.length);



message = " I really do not know ";
console.log(message.length);
message = message.trim();
console.log(message.trim());
console.log(message.length);
6 changes: 6 additions & 0 deletions exercises/G-numbers/exercise.js
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
// Start by creating a variables `numberOfStudents` and `numberOfMentors`
let numberOfStudents = 15;
let numberOfMentors = 8;

console.log(`Number of Students: ${numberOfStudents}\n
Number of mentors ${numberOfMentors}\n
Total no. of students and mentors ${numberOfMentors+numberOfStudents}`);
9 changes: 9 additions & 0 deletions 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 totalStMe = numberOfMentors + numberOfStudents;

function percentage(no1, no2){
outPut = Math.round((no2/no1)*100);
return outPut
}

console.log(`Percentage students: ${percentage(totalStMe, numberOfStudents)}%`);
console.log(`Percentage mentors: ${percentage(totalStMe, numberOfMentors)}%`);
2 changes: 2 additions & 0 deletions exercises/J-functions/exercise.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
function halve(number) {
// complete the function here

return number / 2
}

var result = halve(12);
Expand Down
1 change: 1 addition & 0 deletions 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 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(n1, n2) {
// Calculate the result of the function and return it
return n1 * n2
}

// Assign the result of calling the function the variable `result`
Expand Down
3 changes: 3 additions & 0 deletions 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(n1, n2){
return (n1 / n2)
}

var result = divide(3, 4);

Expand Down
6 changes: 6 additions & 0 deletions exercises/K-functions-parameters/exercise3.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
// Write your function here
function createGreeting(n2){
let n1 = "Hello, my name is ";
return (n1 + n2)
}


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 sumOperator(n1, n2){
return (n1 + n2)
}
// Call the function and assign to a variable `sum`

let sum = sumOperator(13, 124);
console.log(sum);
5 changes: 4 additions & 1 deletion exercises/K-functions-parameters/exercise5.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// Declare your function here

function createLongGreeting(n1, n2){

return (`Hello, my name is ${n1} and I'm ${n2} years old.`);
}
const greeting = createLongGreeting("Daniel", 30);

console.log(greeting);
28 changes: 17 additions & 11 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,25 +17,29 @@ 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(currency) {
return currency * 5.7 * 0.99;
}

/* ======= 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 1-currency-conversion` into your terminal
*/

const util = require('util');
const util = require("util");

function test(test_name, actual, expected) {
let status;
if (actual === expected) {
status = "PASSED";
} else {
status = `FAILED: expected: ${util.inspect(expected)} but your function returned: ${util.inspect(actual)}`;
}

console.log(`${test_name}: ${status}`);
let status;
if (actual === expected) {
status = "PASSED";
} else {
status = `FAILED: expected: ${util.inspect(
expected
)} but your function returned: ${util.inspect(actual)}`;
}

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

test("convertToUSD function works", convertToUSD(32), 44.8);
Expand Down
55 changes: 31 additions & 24 deletions extra/2-piping.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,49 +16,56 @@
the final result to the variable goodCode
*/

function add() {

function add(a, b) {
if (isNaN(a) && isNaN(b)) return console.log("It is not a number");
return a + b;
}

function multiply() {

function multiply(a, b) {
if (isNaN(a) && isNaN(b)) return console.log("It is not a number");
return a * b;
}

function format() {

function format(a) {
if (isNaN(a) && isNaN(b)) return console.log("It is not a number");
return `£${a}`;
}

const startingValue = 2
// I'm not getting it?

const startingValue = 2;

// Why can this code be seen as bad practice? Comment your answer.
let badCode =
let badCode = "£24";

/* BETTER PRACTICE */

let goodCode =
let goodCode = "£24";

/* ======= 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-piping.js` into your terminal
*/

const util = require('util');
const util = require("util");

function test(test_name, actual, expected) {
let status;
if (actual === expected) {
status = "PASSED";
} else {
status = `FAILED: expected: ${util.inspect(expected)} but your code returned: ${util.inspect(actual)}`;
}

console.log(`${test_name}: ${status}`);
let status;
if (actual === expected) {
status = "PASSED";
} else {
status = `FAILED: expected: ${util.inspect(
expected
)} but your code returned: ${util.inspect(actual)}`;
}

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

test('add function - case 1 works', add(1,3), 4)
test('add function - case 2 works', add(2.4,5), 7.4)
test('multiply function works', multiply(2,3), 6)
test('format function works', format(16), "£16")
test('badCode variable correctly assigned', badCode, "£24")
test('goodCode variable correctly assigned', goodCode, "£24")
test("add function - case 1 works", add(1, 3), 4);
test("add function - case 2 works", add(2.4, 5), 7.4);
test("multiply function works", multiply(2, 3), 6);
test("format function works", format(16), "£16");
test("badCode variable correctly assigned", badCode, "£24");
test("goodCode variable correctly assigned", goodCode, "£24");
57 changes: 48 additions & 9 deletions extra/3-magic-8-ball.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,40 @@ My sources say no.
Outlook not so good.
Very doubtful.
*/
const sentenceArr = [
//0-4
"It is certain.",
"It is decidedly so.",
"Without a doubt.",
"Yes - definitely.",
"You may rely on it.",
//4-9
"As I see it, yes.",
"Most likely.",
"Outlook good.",
"Yes.",
"Signs point to yes.",
//10-14
"Reply hazy, try again.",
"Ask again later.",
"Better not tell you now.",
"Cannot predict now.",
"Concentrate and ask again.",
//15 - 19
"Don't count on it.",
"My reply is no.",
"My sources say no.",
"Outlook not so good.",
"Very doubtful.",
];

const { randomBytes } = require("crypto");

// This should log "The ball has shaken!"
// and return the answer.
function shakeBall() {
console.log("The ball has shaken!");
return sentenceArr[Math.floor(Math.random() * 20)];
}

// This function should say whether the answer it is given is
Expand All @@ -55,6 +85,18 @@ function shakeBall() {
// - very negative
// This function should expect to be called with any value which was returned by the shakeBall function.
function checkAnswer(answer) {
if (sentenceArr.indexOf(answer) >= 0 && sentenceArr.indexOf(answer) <= 4) {
return "very positive";
}
if (sentenceArr.indexOf(answer) >= 5 && sentenceArr.indexOf(answer) <= 9) {
return "positive";
}
if (sentenceArr.indexOf(answer) >= 10 && sentenceArr.indexOf(answer) <= 14) {
return "negative";
}
if (sentenceArr.indexOf(answer) >= 15 && sentenceArr.indexOf(answer) <= 19) {
return "very negative";
}
}

/* ======= TESTS - DO NOT MODIFY =====
Expand All @@ -65,7 +107,7 @@ To run these tests type `node 3-magic-8-ball.js` into your terminal

const log = console.log;
let logged;
console.log = function() {
console.log = function () {
log(...arguments);
logged = arguments[0];
};
Expand All @@ -92,14 +134,14 @@ function testAll() {
test(`shakeBall returns an string answer`, typeof answer === "string");

test(
`checkAnswer("It is decidedly so.") returns "very positive`,
`checkAnswer("It is decidedly so.") returns "very positive"`,
checkAnswer("It is decidedly so.") === "very positive"
)
);

test(
`checkAnswer("My reply is no.") returns "very negative`,
checkAnswer("My reply is no.") === "very negative"
)
);

test(
`checkAnswer returns the level of positivity"`,
Expand All @@ -111,13 +153,10 @@ function testAll() {
for (let i = 0; i < 10; ++i) {
answers.add(shakeBall());
}
test(
`shakeBall returns different answers`,
answers.size > 1,
);
test(`shakeBall returns different answers`, answers.size > 1);
test(
`checkAnswer returns different answers`,
new Set(Array.from(answers.values()).map(checkAnswer)).size > 1,
new Set(Array.from(answers.values()).map(checkAnswer)).size > 1
);
}

Expand Down
11 changes: 6 additions & 5 deletions mandatory/1-syntax-errors.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
// 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;

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

/* ======= TESTS - DO NOT MODIFY =====
Expand Down
Loading