Skip to content
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: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules
.DS_Store
.vscode
**/.DS_Store
**/.DS_Store
package-lock.json
8 changes: 7 additions & 1 deletion Sprint-1/1-key-exercises/1-count.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
let count = 0;

/* Incrementing by 1 the count value. */
count = count + 1;
/* Check if count === 1. */
console.log(count);
/* Incrementing by 1 the count value. */
count = count + 1;
/* Check if count === 2. */
console.log(count);

// Line 1 is a variable declaration, creating the count variable with an initial value of 0
// Describe what line 3 is doing, in particular focus on what = is doing
6 changes: 4 additions & 2 deletions Sprint-1/1-key-exercises/2-initials.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ let lastName = "Johnson";

// Declare a variable called initials that stores the first character of each string.
// This should produce the string "CKJ", but you must not write the characters C, K, or J in the code of your solution.

let initials = ``;
/* So many ways to get the first char of a string ! */
let initials = `${firstName.charAt(0)}${middleName[0]}${lastName.slice(0, 1)}`;
/* Check if ok */
console.log(initials);

// https://www.google.com/search?q=get+first+character+of+string+mdn

15 changes: 10 additions & 5 deletions Sprint-1/1-key-exercises/3-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@ const lastSlashIndex = filePath.lastIndexOf("/");
const base = filePath.slice(lastSlashIndex + 1);
console.log(`The base part of ${filePath} is ${base}`);

// Create a variable to store the dir part of the filePath variable
// Create a variable to store the ext part of the variable
// Create a variable to store the directories part of the filePath variable.
// Create a variable to store the extension part of the variable.

const dir = ;
const ext = ;
/* Funny ! */
const dir = filePath.slice(0, lastSlashIndex + 1);
const lastPointIndex = filePath.lastIndexOf(".");
const ext = filePath.slice(lastPointIndex + 1);
/* Check */
console.log("The directories part of the filePath is : " + dir);
console.log("The extension part of the variable is : " + ext);

// https://www.google.com/search?q=slice+mdn
// https://www.google.com/search?q=slice+mdn
30 changes: 30 additions & 0 deletions Sprint-1/1-key-exercises/4-random.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,37 @@
const minimum = 1;
const maximum = 100;

/*
* Well-known JavaScript pattern for generating random integers
* in a specific range [minimum, maximum] (both inclusive).
* minimum <= num <= maximum.
*/
/* Shift along the range from min to max. */
const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
/* Check */
let counter = 0;
const min = 5;
const max = 20;

while (counter <= 5) {
console.log("\nMinimum", min, "Maximum", max);
// Mystery of Mathematics.
const m = max - min + 1;
console.log("(max - min + 1) => ", m);
/* return random float in between 0 and 1 (non inclusive) */
const rd = Math.random();
console.log("random", rd);
// Mystery of Mathematics.
console.log(`(${m} * random) => `, m * rd);
/* Math.floor return the nearest lower integer
* E.g. Math.floor(3.9) === 3;
* Return a range 0 => 99.
*/
const int = Math.floor(rd * m);
console.log(`Math.floor(${m} * random) =>`, int);
console.log(`${int} + ${min} =>`, Math.floor(rd * m) + min);
counter++;
}

// In this exercise, you will need to work out what num represents?
// Try breaking down the expression and using documentation to explain what it means
Expand Down
7 changes: 5 additions & 2 deletions Sprint-1/2-mandatory-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
This is just an instruction for the first activity - but it is just for human consumption
We don't want the computer to run these 2 lines - how can we solve this problem?
/* This is just an instruction for the first activity - but it is just for human consumption
We don't want the computer to run these 2 lines - how can we solve this problem? */
/*
* Just comment it.
*/
7 changes: 5 additions & 2 deletions Sprint-1/2-mandatory-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// trying to create an age variable and then reassign the value by 1

const age = 33;
/* TypeError: Assignment to constant variable. */
// const age = 33;
let age = 33;
age = age + 1;
/* Check if ok. */
console.log(age);
4 changes: 3 additions & 1 deletion Sprint-1/2-mandatory-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Currently trying to print the string "I was born in Bolton" but it isn't working...
// what's the error ?

console.log(`I was born in ${cityOfBirth}`);
const cityOfBirth = "Bolton";
console.log(`I was born in ${cityOfBirth}`);
/* Cannot access 'cityOfBirth' before initialization */
// const cityOfBirth = "Bolton";
13 changes: 12 additions & 1 deletion Sprint-1/2-mandatory-errors/3.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
const cardNumber = 4533787178994213;
const last4Digits = cardNumber.slice(-4);
/* cardNumber is a number but slice() is a String method. */
/* TypeError: cardNumber.slice is not a function. */
//const last4Digits = cardNumber.slice(-4);
const last4Digits = cardNumber.toString().slice(-4);
/* Another way to cast a number (string + number == string) */
const otherLast4Digits = `${cardNumber}`.toString().slice(-4);
const anotherLast4Digits = ("" + cardNumber).slice(-4);
/* Check if ok */
console.log(last4Digits);
console.log(otherLast4Digits);
console.log(anotherLast4Digits);


// The last4Digits variable should store the last 4 digits of cardNumber
// However, the code isn't working
Expand Down
11 changes: 9 additions & 2 deletions Sprint-1/2-mandatory-errors/4.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
const 12HourClockTime = "20:53";
const 24hourClockTime = "08:53";
/* Js variables are not allowed to start with digit */
/* SyntaxError: Invalid or unexpected token */
// const 12HourClockTime = "20:53";
// const 24hourClockTime = "08:53";
const hourClockTime12 = "20:53";
const hourClockTime24 = "08:53";
/* Check if ok. */
console.log("hourClockTime12 : " + hourClockTime12);
console.log("hourClockTime24 : " + hourClockTime24);
33 changes: 27 additions & 6 deletions Sprint-1/3-mandatory-interpret/1-percentage-change.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,42 @@ let carPrice = "10,000";
let priceAfterOneYear = "8,543";

carPrice = Number(carPrice.replaceAll(",", ""));
priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));
priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",",""));

const priceDifference = carPrice - priceAfterOneYear;
const percentageChange = (priceDifference / carPrice) * 100;

console.log(`The percentage change is ${percentageChange}`);
console.log(`The percentage change is ${percentageChange} %`);

// Read the code and then answer the questions below

// a) How many function calls are there in this file? Write down all the lines where a function call is made
// a) How many function calls are there in this file?
// Write down all the lines where a function call is made.
/*
* line 4 & 5 => replaceAll() - String method.
* line 4 & 5 => Number() - built-in constructor function
* line 10 => console.log() - built-in constructor function
*/

// b) Run the code and identify the line where the error is coming from - why is this error occurring? How can you fix this problem?
// b) Run the code and identify the line where the error is coming from
// why is this error occurring? How can you fix this problem?
/*
* line 5 => Miss a comma in arguments declaration.
*/

// c) Identify all the lines that are variable reassignment statements
/*
* lines 4 & 5 => carPrice & priceAfterOneYear are reassigned.
*/

// d) Identify all the lines that are variable declarations

// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
/*
* lines 1 & 2 => carPrice & priceAfterOneYear.
* lines 7 & 8 => priceDifference & percentageChange.
*/

// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing
// what is the purpose of this expression?
/*
* Cast a string to Number.
*/
36 changes: 31 additions & 5 deletions Sprint-1/3-mandatory-interpret/2-time-format.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const movieLength = 8784; // length of movie in seconds
const movieLength = -8784; // length of movie in seconds

const remainingSeconds = movieLength % 60;
const totalMinutes = (movieLength - remainingSeconds) / 60;
Expand All @@ -12,14 +12,40 @@ console.log(result);
// For the piece of code above, read the code and then answer the following questions

// a) How many variable declarations are there in this program?
/*
* 6 variable's declarations:
* movieLength, remainingSeconds, totalMinutes, remainingMinutes, totalHours, result.
*/

// b) How many function calls are there?
/*
* Only console.log()
*/

// c) Using documentation, explain what the expression movieLength % 60 represents
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators
/*
* Modulo (%) is use to return the rest number of a division.
* 8784 % 60 = 8784 - (60 × Math.floor(8784 / 60));
*/
console.log( "\nModule explained");
console.log("8784 % 60 =" ,8784 - (60 * Math.floor(8784 / 60)));

// d) Interpret line 4, what does the expression assigned to totalMinutes mean?

// e) What do you think the variable result represents? Can you think of a better name for this variable?

// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
/*
* The rest number are the seconds.
* We substract them to the time length then timelength / 60 result
* is minutes (60 seconds in a minute).
*/

// e) What do you think the variable result represents?
// Can you think of a better name for this variable?
/* getClockTime() */

// f) Try experimenting with different values of movieLength.
// Will this code work for all values of movieLength? Explain your answer
/*
* Yes but the result display is not optimal in case
* movieLength = 0 ormovieLength < 0 or totalHours === 0 or some of the value < 10.
*/
33 changes: 33 additions & 0 deletions Sprint-1/3-mandatory-interpret/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const pounds = paddedPenceNumberString.substring(
0,
paddedPenceNumberString.length - 2
);
console.log(paddedPenceNumberString.charAt(0));


const pence = paddedPenceNumberString
.substring(paddedPenceNumberString.length - 2)
Expand All @@ -25,3 +27,34 @@ console.log(`£${pounds}.${pence}`);

// To begin, we can start with
// 1. const penceString = "399p": initialises a string variable with the value "399p"
/*
* 2. const penceStringWithoutTrailingP = penceString.substring( penceString.length - 1);
* Assign the penceStringWithoutTrailingP variable to string penceString trailing last char of it.
* length of penceString - 1 because index begins to 0
* String method substring() parameter does not accept negative index (unlike slice)
* String method substring() returns charAt(3) to the end.
* penceString is not muted (remains the same).
*
* 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
* penceStringWithoutTrailingP.padStart(3, "0") = add (number - string.length) "0"
* Here penceStringWithoutTrailingP.length = 3 so 3 - 3 = nothing to add.
* useful to add 0 if the price < 1 pound (Impossible nowadays).

* 4. const pounds = paddedPenceNumberString.substring( 0, paddedPenceNumberString.length - 2);
* String method substring() returns charAt(0) to charAt(1)(3- 1) non inclusif,
* Equivalent to substring( 0, 1);
* Equivalent to paddedPenceNumberString.charAt(0);

* 5. const pence = paddedPenceNumberString
.substring(paddedPenceNumberString.length - 2)
.padEnd(2, "0");
* String method substring() returns the 2 last char of the string.
* Here the pences.
* padEnd(2, "0") same system than above but add "0" at the end if the string.length < 2
* E.g. "9" => "90".

* 6. console.log(`£${pounds}.${pence}`);
* Display in the console currency + pound's value + separator + pence's value
* thanks to the template literals ``.

*/
4 changes: 4 additions & 0 deletions Sprint-1/4-stretch-explore/objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ Answer the following questions:

What does `console` store?
What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean?
- console.log(["message"], value.toString())
- console.assert(assertion, "message", [optionalParams]);
- - assertion returns a boolean (false / true). If false the message will be displayed in the console.
- - E.g. console.assert(5 < 2, "You are really dumb into mathematics");
13 changes: 8 additions & 5 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
// Predict and explain first...
// =============> write your prediction here
// Guess init a variable with the same name than the argument is not a good idea.

// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring

function capitalise(str) {
/* function capitalise(str) {
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
}
} */

// =============> write your explanation here
// =============> write your new code here
// SyntaxError: Identifier 'str' has already been declared
function capitalise(str) {
return `${str[0].toUpperCase()}${str.slice(1)}`;
}
console.log(capitalise("hello world"));
17 changes: 12 additions & 5 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
// Predict and explain first...

// Why will an error occur when this program runs?
// =============> write your prediction here
// Variable 'decimalNumber' is already declared. And this declaration is no needed.
// console.log(decimalNumber); will throw an error. const is block scoped.

// Try playing computer with the example to work out what is going on

function convertToPercentage(decimalNumber) {
/* function convertToPercentage(decimalNumber) {
const decimalNumber = 0.5;
const percentage = `${decimalNumber * 100}%`;

return percentage;
}

console.log(decimalNumber);
console.log(decimalNumber); */

// =============> write your explanation here
// SyntaxError: Identifier 'decimalNumber' has already been declared

// Finally, correct the code to fix the problem
// =============> write your new code here
function convertToPercentage(decimalNumber) {
const percentage = `${decimalNumber * 100}%`;
return percentage;
}

console.log(convertToPercentage(0.5));

17 changes: 11 additions & 6 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,23 @@

// this function should square any number but instead we're going to get an error

// =============> write your prediction of the error here
// '3' is a number not an argument.

function square(3) {
/* function square(3) {
return num * num;
}
} */

// =============> write the error message here
// SyntaxError: Unexpected number

// =============> explain this error message here
// '3' is a number not an argument.
// An argument is a variable and must not begin with a number.
// the var 'num' is coming from nowhere.

// Finally, correct the code to fix the problem

// =============> write your new code here
function square(num) {
return num * num;
}

console.log(square(3));

Loading