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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

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`
- To run only the tests for the mandatory exercises, run `npm test -- --selectProjects mandatory`
- To run only the tests for the extra exercises, run `npm test -- --selectProjects extra`
- To run only the tests for the extra exercises, run `npm test -- --selectProjects extra`e
- To run a single exercise/test (for example `mandatory/1-writer.js`), run `npm test -- --testPathPattern mandatory/1-writer.js` (Remember, you can use tab-completion to get files relative to the current directory, so m`Tab ↹`/1-`Tab ↹` will autocomplete get you the test file starting with 1-)

For more information about tests, look here:
Expand Down
2 changes: 1 addition & 1 deletion exercises/B-hello-world/exercise.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
console.log("Hello world");
console.log("Hello World!");
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 message = "Hello, my name is";
var name = " Shafiek";
var greeting = message + name;
console.log(greeting);
14 changes: 13 additions & 1 deletion exercises/F-strings-methods/exercise.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
// Start by creating a variable `message`
var message = "My name is ";
var name = "Shafiek";
var sentence = " and my name is ";
var nameLength = name.length;
var sentenceEnd = " characters long";
var fullSentence = message + name + sentence + nameLength + sentenceEnd;

console.log(message);
console.log(fullSentence);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nice work Shafiek 🔥
Works as expected, but maybe not all the parts of the string needs to be seperate variables.
Using template literals will make the code less cluttered and achieve the same result.


// var name = "Daniel";

// var nameLowerCase = name.toLowerCase();

// console.log(nameLowerCase);
2 changes: 1 addition & 1 deletion exercises/F-strings-methods/exercise2.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const name = " Daniel ";

let message = name.trim();
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`
let numberOfStudents = 15;
console.log("Number of students: ", numberOfStudents);
let numberOfMentors = 8;
console.log("Number of mentors: ", numberOfMentors);

let sum = numberOfStudents + numberOfMentors;
console.log("Total number of students and mentors: ", sum);
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 @@
var numberOfStudents = 15;
var numberOfMentors = 8;

let preciseStudentPercentage = (numberOfStudents / 23) * 100;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Works as expected , well done.
As a suggestion, try to avoid magic numbers in your code.
Would the preciseStudentPercentage still be correct if the numberOfStudents or numberOfMentors changes ?
In this scenario, 23 is a "magic number". We know that it works now because that is the total of students + mentors, but what can we do to make it work if those values change?

roundedStudentPercentage = Math.round(preciseStudentPercentage);
let preciseMentorPercentage = (numberOfMentors / 23) * 100;
roundedMentorPercentage = Math.round(preciseMentorPercentage);

console.log("Percentage students: ", roundedStudentPercentage, "%");
console.log("Percentage mentors: ", roundedMentorPercentage, "%");
3 changes: 2 additions & 1 deletion exercises/J-functions/exercise.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
function halve(number) {
// complete the function here
return number / 2;
}

var result = halve(12);
var result = halve(55);

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

var result = triple(12);
var result = triple(9);

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

// 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(num1, num2) {
return num1 / num2;
}
var result = divide(3, 4);

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

function createGreeting(name) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggested change
function createGreeting(name) {
function createGreeting(name) {
// function createGreeting should also include the static text "Hello, my name is "

return name;
}

var greeting = createGreeting("Daniel");

console.log(greeting);
console.log("Hello, my name is", greeting);
6 changes: 6 additions & 0 deletions exercises/K-functions-parameters/exercise4.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
// Declare your function first
let num1 = 13;
let num2 = 124;

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

console.log(sum);
7 changes: 4 additions & 3 deletions exercises/K-functions-parameters/exercise5.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Declare your function here

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

console.log(greeting);
function createLongGreeting(name, age) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Modifying the code from exercise3.js in this folder, can you modify createLongGreeting to return a greeting with both the name ( a string ) and an age ( a number ) ?

if (name === "" && age === number);
return name, age;
}
13 changes: 12 additions & 1 deletion exercises/L-functions-nested/exercise.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
var mentor1 = "Daniel";
let mentor1 = "Daniel";

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

That's right!! 👍
Avoid var !! Thanks for that update Shafiek

var mentor2 = "Irina";
var mentor3 = "Mimi";
var mentor4 = "Rob";
var mentor5 = "Yohannes";

// function mentor(name) {
// return (name = mentor1.toUpperCase());
// }
// console.log(mentor1);
let name = mentor1.toUpperCase();

console.log(name);

// let name = mentor2.toUpperCase();
// console.log(name);
14 changes: 7 additions & 7 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;
let total = a + b;

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

/*
Expand All @@ -19,7 +19,7 @@ function getTotal(a, b) {

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 1-syntax-errors` into your terminal
To run the tests for just this one file, type `clear` into your terminal
(Reminder: You must have run `npm install` one time before this will work!)

===================================================
Expand Down
7 changes: 3 additions & 4 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 Down
6 changes: 6 additions & 0 deletions mandatory/3-function-output.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
// Add comments to explain what this function does. You're meant to use Google!
//Explanation of this function--
//Math.random will generate a random number between 0 and 1, so the function will generate a random number between 0 and 1 and then multiply it by 10
function getRandomNumber() {
return Math.random() * 10;
}
console.log(getRandomNumber());

// Add comments to explain what this function does. You're meant to use Google!
//Explanation of this function--
//This function will concatenate 2 strings
function combine2Words(word1, word2) {
return word1.concat(word2);
}

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 this function is expected to return.
return firstWord.concat(" ", secondWord).concat(" ", thirdWord);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nice use of chaining functions, well done

}

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

function calculateSalesTax() {}
function calculateSalesTax(price) {
let taxincl = price * 1.2;
return taxincl;
}

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

function addTaxAndFormatCurrency() {}
function addTaxAndFormatCurrency(currency) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Using the hint in the comment (hint: you already wrote a function for this!), can you think of another way to calculate the tax in the addTaxAndFormatCurrency function? Remember, functions are written to be reusable and to prevent us from rewriting logic

total = currency * 1.2;
return "£" + total.toFixed(2);
}

/*
===================================================
Expand Down
12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,21 @@
"url": "https://github.com/CodeYourFuture/JavaScript-Core-1-Coursework-Week1/issues"
},
"jest": {
"setupFilesAfterEnv": ["jest-extended"],
"setupFilesAfterEnv": [
"jest-extended"
],
"projects": [
{
"displayName": "mandatory",
"testMatch": ["<rootDir>/mandatory/*.js"]
"testMatch": [
"<rootDir>/mandatory/*.js"
]
},
{
"displayName": "extra",
"testMatch": ["<rootDir>/extra/*.js"]
"testMatch": [
"<rootDir>/extra/*.js"
]
}
]
},
Expand Down