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 Sprint-1/1-key-exercises/1-count.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
let count = 0;

count = count + 1;

console.log(count); // Output: 1
// 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 an assignment statement that updates the value of the count variable. The expression on the right-hand side (count + 1) calculates the new value by taking the current value of count (which is 0) and adding 1 to it, resulting in 1. The = operator then assigns this new value back to the count variable, effectively updating it from 0 to 1.
3 changes: 2 additions & 1 deletion 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.charAt(0) + middleName.charAt(0) + lastName.charAt(0); // Your code here;
console.log(initials); // Output: "CKJ"//Your code to validate output here

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

6 changes: 4 additions & 2 deletions Sprint-1/1-key-exercises/3-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ 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);
const ext = filePath.slice(lastSlashIndex + 1).split(".").pop();
console.log(`The dir part of ${filePath} is ${dir}`);
console.log(`The ext part of ${filePath} is ${ext}`);

// https://www.google.com/search?q=slice+mdn
6 changes: 6 additions & 0 deletions Sprint-1/1-key-exercises/4-random.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
const minimum = 1;
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

//Num generates a random number 0-0.9999999999999999, then multiplies it by (100-1+1=100), which gives a number between 0 and 99.99999999999999. Then it adds 1, which gives a number between 1 and 100. Finally, it uses Math.floor to round down to the nearest whole number, giving a final result of a random integer between 1 and 100 (inclusive).
4 changes: 3 additions & 1 deletion Sprint-1/2-mandatory-errors/0.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion Sprint-1/2-mandatory-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
// trying to create an age variable and then reassign the value by 1

const age = 33;
//const age = 33;
let age = 33;
age = age + 1;
console.log(age);

//problem: we cannot reassign a value to a variable that has been declared with "const". We can solve this problem by changing the declaration of the variable from "const" to "let", which allows us to reassign values to the variable. So I have commed line 3 and added line 4 to "let age = 33;". Also, I have added a line at the end to log the value of age to the console, so we can see the result of the reassignment to check that it is working correctly.
1 change: 1 addition & 0 deletions Sprint-1/2-mandatory-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@

console.log(`I was born in ${cityOfBirth}`);
const cityOfBirth = "Bolton";
//problem: we are trying to use the variable "cityOfBirth" before it has been declared and assigned a value. In JavaScript, variables declared with "const" or "let" cannot be accessed before they are declared. To fix this error, we need to declare and assign a value to "cityOfBirth" before we try to use it. So we would move the line "const cityOfBirth = "Bolton";" above the console.log statement.
9 changes: 8 additions & 1 deletion Sprint-1/2-mandatory-errors/3.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
const cardNumber = 4533787178994213;
const last4Digits = cardNumber.slice(-4);
//const last4Digits = cardNumber.slice(-4);


// convert number → string, then slice the last 4 chars
const last4 = cardNumber.toString().slice(-4);

console.log(last4); // "4213"

// 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
// cardNumber is a variable that holds a number, and numbers do not have a slice method. The slice method is used for strings and arrays, but not for numbers. To fix this error, we need to convert the cardNumber to a string before using the slice method. We can do this by using the toString() method on cardNumber, like this: cardNumber.toString().slice(-4). This will convert the number to a string and then allow us to use the slice method to get the last 4 digits.
14 changes: 12 additions & 2 deletions Sprint-1/2-mandatory-errors/4.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
const 12HourClockTime = "20:53";
const 24hourClockTime = "08:53";
//const 12HourClockTime = "20:53";
//const 24hourClockTime = "08:53";
// JavaScript variable names (identifiers) must not start with a digit.
// They may only start with:a letter
// (A–Z or a–z)

const T12HourClockTime = "20:53";
const T24hourClockTime = "08:53";
console.log (T12HourClockTime);
console.log (T24hourClockTime);


15 changes: 11 additions & 4 deletions Sprint-1/3-mandatory-interpret/1-percentage-change.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
let carPrice = "10,000";
let priceAfterOneYear = "8,543";

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

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


const priceDifference = carPrice - priceAfterOneYear;
const percentageChange = (priceDifference / carPrice) * 100;
Expand All @@ -12,11 +16,14 @@ 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

// There are 3 function calls in this file. They are on lines 4, 5, and 10. The function calls are: two uses of replaceAll,and one console.log.
// 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 coming from lines 4 and 5 (which I have commed out). The error is occurring because there is a space between the comma and the closing quotation mark in the second argument of the replaceAll function. This causes the function to look for a comma followed by a space, which does not exist in the string. To fix this problem, have removed the space between the comma and the closing quotation mark in both lines, on lines 7&8.
// c) Identify all the lines that are variable reassignment statements
// The variable reassignment statements are on lines 7 and 8, where carPrice and priceAfterOneYear are being reassigned to the result of the replaceAll function calls.

// d) Identify all the lines that are variable declarations
// The variable declarations are on lines 1, 2, 11 and 12. They are: carPrice, priceAfterOneYear, priceDifference, and percentageChange.

// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
// The expression Number(carPrice.replaceAll(",","")) is first using the replaceAll function to remove all commas from the carPrice string, resulting in a string of digits. Then, the Number function is converting that string of digits into a numerical value. The purpose of this expression is to convert the carPrice from a formatted string (with commas) into a number that can be used for calculations.
9 changes: 8 additions & 1 deletion Sprint-1/3-mandatory-interpret/2-time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,21 @@ 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?

// There are 6 variable declarations in this program. They are: movieLength, remainingSeconds, totalMinutes, remainingMinutes, totalHours, and result.
// b) How many function calls are there?
// There are 2 function calls in this program. They are: the template literal function call when assigning a value to the result variable (line 9) and the console.log function call on line 10.

// c) Using documentation, explain what the expression movieLength % 60 represents
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators
// The expression movieLength % 60 calculates the remainder when movieLength is divided by 60. This gives us the number of seconds that are left over after converting the total seconds into minutes. Movilength is 146 minutes and 24 seconds, so the remaining seconds is 24.

// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
// The expression (movieLength - remainingSeconds) / 60 calculates the total number of minutes in the movie, excluding the remaining seconds, based on the movilength (8784s)so the calculation is; (8784-24)/60 = 146 minutes.


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

// The variable result is the length of the movie in hours, minutes, and seconds. A better name for this variable could be movieDurationhms, as it more clearly indicates that it is the duration of the movie in hours, minutes, and seconds.

// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
// Movie length will work for all integers, based it it being a real movie and therefore non negative and the timing device only able to measure whole seconds. If movieLength is not an integer (e.g., a floating-point number), the calculations may yield unexpected results due to the way JavaScript handles division and modulus operations with non-integer values.
5 changes: 5 additions & 0 deletions Sprint-1/3-mandatory-interpret/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,8 @@ console.log(`£${pounds}.${pence}`);

// To begin, we can start with
// 1. const penceString = "399p": initialises a string variable with the value "399p"
//3-5. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1): creates a new string variable that contains the original string without the last character (the "p"), resulting in "399"
//8. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"): pads the string with leading zeros to ensure it has at least 3 characters, resulting in "399"
//9-11. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2): extracts the substring representing the pounds by taking all characters except the last two, resulting in "3"
//14. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"): extracts the last two characters to represent the pence and pads it with trailing zeros if necessary, resulting in "99"
//18. console.log(`£${pounds}.${pence}`): outputs the final formatted price in pounds and pence, resulting in "£3.99"