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
2 changes: 2 additions & 0 deletions Sprint-1/1-key-exercises/1-count.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ count = count + 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 updates the value of count.
// The = means “set count to a new value”, which is the old value plus 1.
4 changes: 2 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,7 @@ 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];
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);
const ext = base.slice(base.lastIndexOf(".") + 1);

// https://www.google.com/search?q=slice+mdn
console.log(dir);
console.log(ext);

// https://www.google.com/search?q=slice+mdn
20 changes: 20 additions & 0 deletions Sprint-1/1-key-exercises/4-random.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,23 @@ const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
// 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

// Math.random() generates a random decimal number between 0 - inclusive and 1 (exclusive)
const randomDecimal = Math.random();
console.log("randomDecimal:", randomDecimal);
// Calculate how many numbers are in the range between minimum and maximum (inclusive)
const range = maximum - minimum + 1;
console.log("range:", range);
// Scale the random decimal to fit within the range...
const scaledNumber = randomDecimal * range;
console.log("scaledNumber:", scaledNumber);
// Round the scaled number down to the nearest whole number
const wholeNumber = Math.floor(scaledNumber);
console.log("wholeNumber:", wholeNumber);
// Shift the number so it starts from the minimum value...
const finalNumber = wholeNumber + minimum;
console.log("finalNumber:", finalNumber);

// The variable num is a random whole number between minimum and maximum - inclusive
console.log("num:", num);

4 changes: 2 additions & 2 deletions Sprint-1/2-mandatory-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
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?
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);

5 changes: 3 additions & 2 deletions Sprint-1/2-mandatory-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Currently trying to print the string "I was born in Bolton" but it isn't working...
// what's the error ?
// The error is caused by using the variable before it is declared
// and by using single quotes instead of backticks for a template string

console.log(`I was born in ${cityOfBirth}`);
const cityOfBirth = "Bolton";
console.log(`I was born in ${cityOfBirth}`);
11 changes: 10 additions & 1 deletion Sprint-1/2-mandatory-errors/3.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
const cardNumber = 4533787178994213;
const last4Digits = cardNumber.slice(-4);
const last4Digits = cardNumber.toString().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

// Prediction:
// cardNumber is a number, not a string
// The slice method only works on strings (and arrays)
// So calling slice on a number will cause an error

7 changes: 5 additions & 2 deletions Sprint-1/2-mandatory-errors/4.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
const 12HourClockTime = "20:53";
const 24hourClockTime = "08:53";
// The error occurs because variable names cannot start with numbers
// JavaScript identifiers must begin with a letter, $ or _

const twelveHourClockTime = "20:53";
const twentyFourHourClockTime = "08:53";
30 changes: 26 additions & 4 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,43 @@ 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}`);

// 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
// Function calls are when we use () like: something()
// Line 4: carPrice.replaceAll(",", "")
// Line 4: Number(...)
// Line 5: priceAfterOneYear.replaceAll(",", "")
// Line 5: Number(...)
// Line 10: console.log(...)
// Answer: 5 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 comes from the console.log line because it uses single quotes with ${percentageChange}
// ${...} only works inside backticks `...` (template strings)
// Fix: use backticks: console.log(`The percentage change is ${percentageChange}`);

// c) Identify all the lines that are variable reassignment statements
// Reassignment means we change an existing variable's value (no let/const on the line)
// Line 4: carPrice = Number(carPrice.replaceAll(",", ""));
// Line 5: priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
// Answer: I think lines 4 and 5

// d) Identify all the lines that are variable declarations
// Declarations use let or const
// Line 1: let carPrice = "10,000";
// Line 2: let priceAfterOneYear = "8,543";
// Line 7: const priceDifference = carPrice - priceAfterOneYear;
// Line 8: const percentageChange = (priceDifference / carPrice) * 100;
// Answer: lines 1, 2, 7 and 8

// e) Describe what the expression Number(carPrice.replaceAll(",", "")) is doing - what is the purpose of this expression?
// replaceAll(",", "") removes commas from the string (e.g. "10,000" becomes "10000")
// Number(...) converts the cleaned string into a real number so we can do maths with it
// Purpose: turn "10,000" (text) into 10000 (number)

// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
29 changes: 26 additions & 3 deletions Sprint-1/3-mandatory-interpret/2-time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,40 @@ const totalHours = (totalMinutes - remainingMinutes) / 60;
const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`;
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?
// Declarations are lines that use const or let
// const movieLength
// const remainingSeconds
// const totalMinutes
// const remainingMinutes
// const totalHours
// const result
// My answer: 6 variable declarations

// b) How many function calls are there?
// A function call uses parentheses like something(..)
// console.log(result) is a function call
// My answer: 1 function call

// c) Using documentation, explain what the expression movieLength % 60 represents
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators
// % is the remainder operator
// movieLength % 60 gives the leftover seconds after dividing by 60
// (the seconds part that doesn't make a full minute)
// Answer: it represents the remaining seconds

// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
// totalMinutes = (movieLength - remainingSeconds) / 60
// First it removes the leftover seconds so we have an exact number of seconds that fits into whole minutes
// Then it divides by 60 to convert seconds into minutes
// Answer: it calculates the total whole minutes in the movie

// e) What do you think the variable result represents? Can you think of a better name for this variable?
// result is a string that formats the time as hours:minutes:seconds
// Better name: formattedTime or movieDuration

// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
// It works for normal positive numbers (seconds) like 8784
// It will also run for 0 (gives 0:0:0)
// But negative values would produce negative hours/minutes/seconds, which isn't a real time format
// So it assumes movieLength is a non-negative number

53 changes: 53 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,56 @@ console.log(`£${pounds}.${pence}`);

// To begin, we can start with
// 1. const penceString = "399p": initialises a string variable with the value "399p"

// creates a string that represents a price in pence, including the letter "p"
//
// Example: "399p"
//
// This is the original input value we want to convert into pounds.


// 2. const penceStringWithoutTrailingP = ...
// Uses substring to remove the last character ("p") from the string
//
// We take the string from index 0 up to (but not including) the last character
//
// Result: "399"
//
// This leaves us with only the numeric part of the price as a string.


// 3. const paddedPenceNumberString = ...
// padStart ensures the string is at least 3 characters long
//
// This is important for small values like "5p":
// "5" becomes "005"
//
// Result for "399": "399"
// Result for "5p": "005"


// 4. const pounds = ...
// Extracts all characters except the last two
//
// The last two characters represent pence,
// everything before that represents pounds
//
// For "399":
// pounds = "3"


// 5. const pence = ...
// Takes the last two characters of the string
//
// padEnd ensures the pence value is always two digits
//
// For "399":
// pence = "99"


// 6. console.log(`£${pounds}.${pence}`)
// Combines pounds and pence into a formatted price string
//
// Final output:
// £3.99