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
6 changes: 6 additions & 0 deletions 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;

count = count + 1;
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

// Line 3 is reassigning the value of the count variable to the result of the expression count + 1.
// The = operator is the assignment operator, which assigns the value on the right-hand side
// (the result of count + 1) to the variable on the left-hand side (count).
// This increments the value of count by 1.
5 changes: 3 additions & 2 deletions Sprint-1/1-key-exercises/2-initials.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ 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 = ``;
let initials = `${firstName[0]}${middleName[0]}${lastName[0]}`;

// https://www.google.com/search?q=get+first+character+of+string+mdn
console.log(initials);

// https://www.google.com/search?q=get+first+character+of+string+mdn
9 changes: 6 additions & 3 deletions Sprint-1/1-key-exercises/3-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ 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

const dir = ;
const ext = ;
const dir = filePath.slice(0, lastSlashIndex);
console.log(`the dir part of ${filePath} is ${dir}`);

// https://www.google.com/search?q=slice+mdn
const ext = filePath.slice(filePath.lastIndexOf("."));
console.log(`the ext part of ${filePath} is ${ext}`);

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

const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;

console.log(num);

// 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
// It will help to think about the order in which expressions are evaluated
// Try logging the value of num and running the program several times to build an idea of what the program is doing

// when first evaluating the full num expression i got the answer 99
// The second time i got 50 (which means .floor and .random are doing something to change the value of the expression each run)

// The order of operations is as follows:

// 1. The expression inside the parentheses is evaluated first: (maximum - minimum + 1) which gives us 100 - 1 + 1 = 100
// 2. Then Math.random() is called, which generates a random decimal number between 0 and 1.
// 3. The result of Math.random() is multiplied by the result of step 1, giving us a random decimal number between 0 and 100.
// 4. Math.floor() is then called on the result of step 3, which rounds it down to the nearest whole number, giving us a random integer between 0 and 99.
// 5. Finally, the minimum value (1) is added to the result of step 4, giving us a random integer between 1 and 100.

// When num = 99
// math.random gives us 0.98 (or something close to it)
// 0.98 * 100 = 98
// math.floor(98) = 98
// 98 + 1 = 99

// When num = 50
// math.random gives us 0.49 (or something close to it)
// 0.49 * 100 = 49
// math.floor(49) = 49
// 49 + 1 = 50
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?

//We solve the problem by commenting out the lines -
// this means that the computer will ignore them when it runs the program
4 changes: 3 additions & 1 deletion Sprint-1/2-mandatory-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// trying to create an age variable and then reassign the value by 1

const age = 33;
let age = 33;
age = age + 1;

console.log(age);
11 changes: 10 additions & 1 deletion Sprint-1/2-mandatory-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
// 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}`);

//ReferenceError: Cannot access 'cityOfBirth' before initialization

// This is happening because we are trying to use the variable cityOfBirth
// before it has been declared and assigned a value.

// Variables declared with let and const can't be accessed before they are initialized.

// To fix this error, we need to declare and assign a value to cityOfBirth before we try to log it to the console.
12 changes: 11 additions & 1 deletion Sprint-1/2-mandatory-errors/3.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
const cardNumber = 4533787178994213;
const cardNumber = "4533787178994213";
const last4Digits = cardNumber.slice(-4);

console.log(last4Digits);

// The last4Digits variable should store the last 4 digits of cardNumber
// However, the code isn't working
// Before running the code, make and explain a prediction about why the code won't work
// Then run the code and see what error it gives.
// Consider: Why does it give this error? Is this what I predicted? If not, what's different?
// Then try updating the expression last4Digits is assigned to, in order to get the correct value

// initial prediction:
// I don't think the code will work because cardNumber is a number, and the slice method is a string method.

// The error I get is: TypeError: cardNumber.slice is not a function

// This error is happening because the slice method is being called on a number,
// which does not have the slice method. The slice method is only available for strings and arrays.
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";
const twelveHourClockTime = "20:53";
const twentyFourHourClockTime = "08:53";

console.log(twelveHourClockTime, twentyFourHourClockTime);

//SyntaxError: Invalid or unexpected token

// This error is happening because variable names cannot start with a number.
// To fix this error, we can change the variable names to start with a letter instead of a numbers.
25 changes: 24 additions & 1 deletion Sprint-1/3-mandatory-interpret/1-percentage-change.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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;
Expand All @@ -13,10 +13,33 @@ console.log(`The percentage change is ${percentageChange}`);

// a) How many function calls are there in this file? Write down all the lines where a function call is made

// Function calls are determined by the presence of parentheses "()" after a function name.
// Therefore in this file, there are 6 function calls:

// 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?

// The error is occurring on line 5:
// SyntaxError: missing ) after argument list. This is because there is a missing comma between the two arguments
// (search value & replace value) in the replaceAll function.
// To fix this problem, we can add a comma between the two arguments.

// c) Identify all the lines that are variable reassignment statements

// These lines include:
// line 3: let carPrice = "10,000"; to carPrice = Number(carPrice.replaceAll(",", ""));
// line 4: let priceAfterOneYear = "8,543"; to priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));

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

// These lines include:
// line 3: let carPrice = "10,000";
// line 4: let priceAfterOneYear = "8,543";
// line 6: const priceDifference = carPrice - priceAfterOneYear;
// line 7: const percentageChange = (priceDifference / carPrice) * 100;

// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?

// The purpose of this is expression is to search for remove all commas from the string value of carPrice and replace them with an empty string.
// This converts the CarPrice value from a string to a type number, which we can now perform mathematical operations on.
// Since the purpose of this program is to calculate the percentage change in the price of a car.
// We need type number values to perform the necessary calculations.
39 changes: 37 additions & 2 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 = true; // length of movie in seconds

const remainingSeconds = movieLength % 60;
const totalMinutes = (movieLength - remainingSeconds) / 60;
Expand All @@ -13,13 +13,48 @@ console.log(result);

// a) How many variable declarations are there in this program?

// There are 6 variable declarations in this program.

// b) How many function calls are there?

// There is 1 function call in this program.

// c) Using documentation, explain what the expression movieLength % 60 represents
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators

// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
// The expression movieLength % 60 is using the modulus operator (%) to calculate the remainder when movieLength is divided by 60.
// In this context, it is calculating the number of seconds that are left over after accounting for the full minutes in the movie length.

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

// It calculates the total number of minutes in the movie by first subtracting the remaining seconds from the total movie length (in seconds)
// and then dividing the result by 60 (the number of seconds in a minute). This gives us the total number of full minutes in the movie length.

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

// The variable result has the value of a template literal that combines the total hours, remaining minutes, and remaining seconds
// into a string format that represents the length of the movie in hours, minutes, and seconds separated by colons.
// A better name for this variable could be "formattedMovieLength"
// to more clearly indicate that it represents the movie length formatted as hours, minutes, and seconds.

// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer

// Test 1: movieLength = 1
// Expected output: 0:0:1
// Actual output: 0:0:1

// Test 2: movieLength = -1
// Expected output: 0:0:-1
// Actual output: 0:0:-1

// Test 3: movieLength = "3,600"
// Expected output: type error
// Actual output: NaN:NaN:NaN

// Test 4: movieLength = true
// Expected output: 0:0:1
// Actual output: 0:0:1

// The code will work for all values of movieLength that are of type number or can be coerced to a number (like true which is coerced to 1).
// This is because arithmetic operation need to be performed on movieLength, and if it is not a number or cannot be coerced to a number,
// It will result in NaN (Not a Number) for the calculations of totalMinutes, remainingMinutes, totalHours, and ultimately the result variable.
97 changes: 96 additions & 1 deletion Sprint-1/3-mandatory-interpret/3-to-pounds.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const penceString = "399p";
const penceString = "9p";

const penceStringWithoutTrailingP = penceString.substring(
0,
Expand All @@ -25,3 +25,98 @@ 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(0, penceString.length - 1) : intialises a variable
// with the value of the penceString variable with the last character "p" removed.

// a. This is achieved by using the substring method on the penceString variable
// the substring method enables the program to manipulate the penceSting variable by targeting and removing characters in the string.

// b. The first argument of the substring method indicates the starting index; (0) which is 399p[0] = "3"

// c. The second argument of the substring method indicates the ending index (penceString.length - 1)
// (.length) returns the length of the string object which is 4, and (-1) targets the last character in the string which is "p".
//
// d. Therefore, the substring method will return a new string that starts from index 0 and ends at index 3 which is "399"

// e. If const penceString = "1399p" the penceStringWithoutTrailingP variable will have the value "1399"
// because the substring method will indicate the starting index as (0 which = "1")
// and the ending index as (penceString.length - 1) which is 5 - 1 = 4, which is "p"
// therefore it will return a new string that starts from index 0 and ends at index 4 which is "1399"

// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0") : initialises a variable
// with the value of the penceStringWithoutTrailingP variable with padding added to the start of the string
// until it reaches a total length of 3 characters.

// a. This is achieved by using the padStart method on the penceStringWithoutTrailingP variable
// the padStart method enables the program to manipulate the penceStringWithoutTrailingP variable
// by adding padding to the start of the string until it reaches a specified length.

// b. The first argument of the padStart method indicates the target length of the resulting string which is 3 in this case.

// c. The second argument of the padStart method indicates the string to use to fill or pad which is "0" in this case.
//
// d. Therefore, if the penceStringWithoutTrailingP variable has a length of less than 3 characters,
// the padStart method will add "0" characters to the start of the string until it reaches a total length of 3 characters.
// In this case, since penceStringWithoutTrailingP is "399" which already has a length of 3 characters,
// the padStart method will not add any padding and paddedPenceNumberString will also be "399".

// e. if const penceStringWithoutTrailingP = "99" the paddedPenceNumberString variable will have the value "099"
// because the padStart method will add one "0" character to the start of the string until it reaches a total length of 3 characters.

// 4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2) : initialises a variable
// with the value of the paddedPenceNumberString variable with the last two characters removed.

// a. This is achieved by using the substring method on the paddedPenceNumberString variable
// the substring method enables the program to manipulate the paddedPenceNumberString variable by targeting and removing characters in the string.

// b. The first argument of the substring method indicates the starting index; (0) which is 399[0] = "3"

// c. The second argument of the substring method indicates the ending index (paddedPenceNumberString.length - 2)
// (.length) returns the length of the string object which is 3, and (-2) targets the last two characters in the string which is "99"
//
// d. Therefore it will return a new string that starts from index 0 and ends at index 1 which is "3"

// d. If const paddedPenceNumberString = "099" the pounds variable will have the value "0" because the substring method
// will indicate the starting index as (0 which = "0") and the ending index as (paddedPenceNumberString.length - 2)
// which is 3 - 2 = 1, which is "9" therefore it will return a new string that starts from index 0 and ends at index 0 which is "0"

// e. if penceString = "1399p" ; const penceStringWithoutTrailingP = "1399" ; const paddedPenceNumberString = "1399"
// because the padStart method will not add any padding since the length of penceStringWithoutTrailingP is already 4 characters
// which is greater than the target length of 3 characters. therefore the const pounds variable will have the value "13"
// because the substring method will indicate the starting index as (0 which = "1") and the ending index as
// (paddedPenceNumberString.length - 2) which is 4 - 2 = 2, which is "9" therefore it will return a new string that starts from index 0
// and ends at index 1 which is "13"

// 5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0") : initialises a variable
// with the value of the last two characters of the paddedPenceNumberString variable with padding added to the end of the string
// until it reaches a total length of 2 characters.

// a. This is achieved by using the substring method on the paddedPenceNumberString variable to extract the last two characters of the string,
// and then using the padEnd method to add padding to the end of the string until it reaches a specified length.

// b. The first argument of the substring method indicates the starting index which is (paddedPenceNumberString.length - 2)
// which targets the last two characters in the string. The ending index is not provided, so it will extract until the end of the string.
//

// c. The padEnd method is then used on the resulting string from the substring method to add padding to the end of the string
// until it reaches a total length of 2 characters.

// d. The first argument of the padEnd method indicates the target length of the resulting string which is 2 in this case.

// e. The second argument of the padEnd method indicates the string to use to fill or pad which is "0" in this case.

// f. Therefore, if the paddedPenceNumberString variable has a length of less than 2 characters, the padEnd method will add "0" characters
// to the end of the string until it reaches a total length of 2 characters.
// In this case, since paddedPenceNumberString is "399" which has a length of 3 characters,
// the substring method will extract the last two characters "99"
// and then the padEnd method will not add any padding since the length of the resulting string is already 2 characters.
// therefore pence will be "99"

// g. if penceString = "9p" ; const penceStringWithoutTrailingP = "9" ; const paddedPenceNumberString = "009" ;
// const pounds = "0" ; const pence = "09" because the substring method will extract the last two characters "09"
// and then the padEnd method will not add any padding since the length of the resulting string is already 2 characters.
// therefore pence will be "09"

// 6. console.log(`£${pounds}.${pence}`) : outputs the final result to the console in the format of "£pounds.pence"
// where pounds and pence are the values of the pounds and pence variables respectively.