Skip to content
This repository was archived by the owner on Jan 14, 2024. 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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

Like learning a musical instrument, programming requires daily practise.

The exercises are split into three folders: `exercises`, `mandatory` and `extra`. All homework in the `exercise` and `mandatory` section **must** be completed for homework by the following lesson.
The exercises are split into three folders: `exercises`, `mandatory` and `extra`. All homework in the `exercise` and `mandatory` section **must** be completed for homework by the following lesson.

The `extra` folder contains exercises that you can complete to challenge yourself, but are not required for the following lesson.

## Running the code/tests

The files for the mandatory/extra exercises are intended to be run as jest tests.
The files for the mandatory/extra exercises are intended to be run as jest tests.

- Once you have cloned the repository, run `npm install` once in the terminal to install jest (and any necessary dependencies).
- To run the tests for all mandatory/extra exercises, run `npm test`
Expand Down
4 changes: 4 additions & 0 deletions exercises/B-hello-world/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,9 @@ Inside of `exercise.js` there's a line of code that will print "Hello world!".

- Try to `console.log()` something different. For example, 'Hello World. I just started learning JavaScript!'.
- Try to console.log() several things at once.

- What happens when you get rid of the quote marks?
I received a syntax error.

- What happens when you console.log() just a number without quotes?
It prints the number without any error.
3 changes: 2 additions & 1 deletion 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 world, I just started learning JavaScript!");
console.log(54);
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";

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

try to always use const to declare variables; let can be used too but only use if if the variable needs redefining at some point (normally, this won't be the case, which is why we prefer const).

var is now outdated, so try to avoid using it :P

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

var message = "This is a string";
console.log(message);
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`

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

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nice use of a template string!

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 ";

message = name.trim(" ");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

almost - variables in JS need declaring by prefixing the variable name with let or const. This effectively tells JS "Hey, every time you see variableName from this point onwards, it's actually talking about this variable"

console.log(message);
console.log(`My name is ${message} and my name is ${message.length} characters long`);

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`
var numberOfStudents = 15;
var numberOfMentors = 8;
var total = numberOfMentors + numberOfStudents;
console.log(`Number of students: ${numberOfStudents}`);
console.log(`Number of mentors: ${numberOfMentors}`);
console.log(`Total number of students and mentors: ${total}`);
6 changes: 6 additions & 0 deletions exercises/I-floats/exercise.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
var numberOfStudents = 15;
var numberOfMentors = 8;
var total = numberOfMentors + numberOfStudents;
var percentageOfStudents = (numberOfStudents * 100) / total;
var percentageOfMentors = (numberOfMentors * 100) / total;

console.log(`Percentage students: ${Math.round(percentageOfStudents)}%`);
console.log(`Percentage mentors: ${Math.round(percentageOfMentors)}%`);
7 changes: 7 additions & 0 deletions 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(20);
console.log(result);

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

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

var result = triple(12);

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(x, y) {
// Calculate the result of the function and return it
return x * y;
}

// Assign the result of calling the function the variable `result`
Expand Down
5 changes: 5 additions & 0 deletions exercises/K-functions-parameters/exercise2.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
// Declare your function first
function divide(x, y) {
if (y !== 0)
return x / y;
return "It is not possible!"
}

var result = divide(3, 4);

Expand Down
3 changes: 3 additions & 0 deletions 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(str) {
return `Hello, my name is ${str}`;
}

var greeting = createGreeting("Daniel");

Expand Down
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(x, y) {
return x + y;
}
// 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);
15 changes: 15 additions & 0 deletions exercises/L-functions-nested/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,18 @@ var mentor2 = "Irina";
var mentor3 = "Mimi";
var mentor4 = "Rob";
var mentor5 = "Yohannes";

function shutyGreeting(mentor) {
var name = upperCase(mentor);
return `Hello ${name}`;
}

function upperCase(mentor) {
return mentor.toUpperCase();
}

console.log(shutyGreeting(mentor1));
console.log(shutyGreeting(mentor2));
console.log(shutyGreeting(mentor3));
console.log(shutyGreeting(mentor4));
console.log(shutyGreeting(mentor5));
10 changes: 8 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 CONVERSION
Expand All @@ -15,7 +17,11 @@ 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) {
let num = price * 5.643; //5.7 * 0.99 = 5.643
num = num.toFixed(2);
return parseFloat(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
21 changes: 11 additions & 10 deletions extra/2-piping.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,31 @@
the final result to the variable goodCode
*/

function add() {

function add(firstNum, secondNum) {
return firstNum + secondNum;
}

function multiply() {

function multiply(firstNum, secondNum) {
return firstNum * secondNum;
}

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

/* BETTER PRACTICE */

let goodCode =
let result = add(startingValue, 10);
result = multiply(result, 2);
let goodCode = format(result);
Comment on lines +37 to +39

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yup this is certainly better; one more improvement that could be made here though is to use const instead to declare multiple variables e.g.

const addResult = add(startingValue, 10)
const multiplyResult = multiply(addResult, 2)
const formattedResult = format(multiplyResult)

A nice rule of thumb to go by with code-style is to always err on the more verbose side (i.e. it's almost always better to be more explicit about your intentions). The code will give the same result either way, so all your code-style is for is for other developers who'll look at your code, and in that case it's better to make your intentions very obvious.


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

To run the tests for just this one file, type `npm test -- --testPathPattern 2-piping` into your terminal
(Reminder: You must have run `npm install` one time before this will work!)
*/
Expand Down
52 changes: 50 additions & 2 deletions extra/3-magic-8-ball.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,41 @@

// This should log "The ball has shaken!"
// and return the answer.

const { toBeOneOf } = require("jest-extended");

const 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."]];


function shakeBall() {
//Write your code in here

let firstItem = Math.floor(Math.random() * answers.length);
let secondItem = Math.floor(Math.random() * answers[0].length);
console.log("The ball has shaken!");
return answers[firstItem][secondItem];
}

/*
Expand All @@ -58,17 +91,32 @@ function shakeBall() {

This function should expect to be called with any value which was returned by the shakeBall function.
*/


function checkAnswer(answer) {
//Write your code in here

for (let i = 0; i < answers.length; i++)
for (let j = 0; j < answers[0].length; j++)
if (answers[i][j] === answer) {
if (i === 0)
return "very positive";
else if (i === 1)
return "positive";
else if (i === 2)
return "negative";
else
return "very negative";
}
}

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

There are some Tests in this file that will help you work out if your code is working.

To run the tests for just this one file, type `npm test -- --testPathPattern 3-magic-8-ball` into your terminal
To run the tests for just this one file, type `npm test-- --testPathPattern 3 - magic - 8 - ball` into your terminal
(Reminder: You must have run `npm install` one time before this will work!)
==================================
*/
Expand Down
15 changes: 7 additions & 8 deletions mandatory/1-syntax-errors.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// 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;

return "The total is total";
total = a + b;
return `The total is ${total}`;
}

/*
Expand All @@ -31,8 +31,7 @@ test("addNumbers adds numbers correctly", () => {

test("introduceMe function returns the correct string", () => {
expect(introduceMe("Sonjide", 27)).toEqual(
"Hello, my name is Sonjide and I am 27 years old"
);
"Hello, my name is Sonjide and I am 27 years old");
});

test("getTotal returns a string describing the total", () => {
Expand Down
10 changes: 4 additions & 6 deletions 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 getStringLength(word) {
return "word".length();
return word.length;
}

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

/*
Expand All @@ -30,8 +29,7 @@ test("trimWord trims leading and trailing whitespace", () => {

test("trimWord doesn't remove whitespace in the middle of the string", () => {
expect(trimWord(" CodeYourFuture teaches coding ")).toEqual(
"CodeYourFuture teaches coding"
);
"CodeYourFuture teaches coding");
});

test("getStringLength returns the length of a word", () => {
Expand Down
Loading