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: 3 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,6 @@ 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 is reassigning the variable 'count' to a new value. The new value that 'count' will take on is the existing value plus 1.
// Given count is assigned a value of 0 in line 1, after running line 3 the value of count would be expected to be 1.
7 changes: 4 additions & 3 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 = ``;

// https://www.google.com/search?q=get+first+character+of+string+mdn
let initials = firstName.charAt(0)
+ middleName.charAt(0)
+ lastName.charAt(0);

// https://www.google.com/search?q=get+first+character+of+string+mdn
12 changes: 10 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,15 @@ 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 lastDotIndex = filePath.lastIndexOf(".");

const dir = filePath.slice(1, lastSlashIndex);
const ext = filePath.slice(lastDotIndex);

// Checks

console.log(`The directory part of ${filePath} is ${dir}`);
console.log(`The extension part of ${filePath} is ${ext}`);


// https://www.google.com/search?q=slice+mdn
42 changes: 42 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,45 @@ 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


console.log(num)

// num returns a value of 60, running it again returns a value of 15, looking at the code I see a .random() function
// I can make a hypothesis that this returns a random number of some kind, hence the inconsistent result

// Let's find out...
// Code in inner-most brackets is evaluated first
// I would expect (maximum - minimum + 1) to be evaluated first (100 - 1 + 1) would equal 100

console.log(maximum - minimum + 1)

// the result was 100
// this part of the code is then multiplied by Math.random()

console.log(Math.random())

// running this code many times, it appears Math.random() returns a random number between 0 and 1
// checking documentation online, this is correct (actually between 0 and 0.99999999999)
// therefore Math.random() * (maximum - minimum + 1) should return a random number between 1 and 100

console.log(Math.random() * (maximum - minimum + 1))

// It does, it returns it with many decimal places, the result of num didn't have any decimal places
// The next part of the code that is run is Math.floor, let's see what this does

const number = Math.random() * (maximum - minimum + 1)
console.log(number)
console.log(Math.floor(number))

// because Math.random() creates a new random number every time it is run, I had to set the
// result to a variable so I can use the same number in multiple functions
// When number is 87.52, Math.floor is 87, when number is 70.02, math.floor is 70
// The function Math.floor appears to be rounding down
// Checking the documentation, this is true, it returns the largest integer <= the given number

// Therefore Math.floor(Math.random() * (maximum - minimum + 1)) must give random numbers between 0 and 99
// The final part is the addition of minimum (+1)

// Therefore I can see this code produces a random whole integer between minimum and maximum
// Which in this case is an integer between 1 and 100
6 changes: 4 additions & 2 deletions Sprint-1/2-mandatory-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
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?

// I commented out the code so it is not run by the computer using '//'
5 changes: 5 additions & 0 deletions Sprint-1/2-mandatory-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@

const age = 33;
age = age + 1;

// The error is TypeError: Assignment to constant variable.
// This is occurring as a constant variable cannot be reassigned
// line 4 is trying to reassign the constant variable age
// This could be navigated by using let instead of const, or assigning a new variable name for age + 1
5 changes: 5 additions & 0 deletions Sprint-1/2-mandatory-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@

console.log(`I was born in ${cityOfBirth}`);
const cityOfBirth = "Bolton";

// error message: ReferenceError: Cannot access 'cityOfBirth' before initialization
// this is occurring as the computer runs the code from top to bottom
// the variable cityOfBirth is assigned in line 5 but is trying to be run in line 4
// this could be fixed by swapping the lines around
14 changes: 13 additions & 1 deletion Sprint-1/2-mandatory-errors/3.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
const cardNumber = 4533787178994213;
const last4Digits = cardNumber.slice(-4);
const last4Digits = String(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

// .slice needs to act on a string not a number, so I would expect something like 'incorrect input' as an error

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

// TypeError: cardNumber.slice is not a function
// This is not exactly the words I expected the error to say but is correctly identifying the same error
// I think it is saying that cardNumber.slice is not a function of a number, hence the TypeError

// Then try updating the expression last4Digits is assigned to, in order to get the correct value

// I converted cardNumber to a string and error resolved and it return the correct output
6 changes: 5 additions & 1 deletion Sprint-1/2-mandatory-errors/4.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
const 12HourClockTime = "20:53";
const 24hourClockTime = "08:53";
const 24hourClockTime = "08:53";

// The error: SyntaxError: Invalid or unexpected token
// This occurs as a variable cannot start with a number (it can include a number)
// Renaming to something like ClockTime12hour would solve this
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

// There are 5 function calls, 2x (lines 4 and 5), 1x (line 10)

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


// Error: SyntaxError: missing ) after argument list (Line 5)
// The arguments in the replaceAll function need to be comma separated, the difference can be seen from line 4 where the code
// did not hit an error, adding in that comma will solve the error

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

// Lines 4 and 5

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

// Lines 1, 2, 7 and 8

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

// It looks like the function is removing the commas (by replacing them with nothing) from the string
// so there are only numbers left and then converting it into a Number data type

console.log(carPrice)

// I commented out the line of code with an error and ran the code to confirm, it returned 10000
// I can check it is a number by running a math operation on it

console.log(carPrice*2)

// result as expected
24 changes: 23 additions & 1 deletion 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 = 5.345345; // length of movie in seconds

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

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

// 6

// b) How many function calls are there?

// 1

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

// % is a modulus operator
// It acts as a divisor, but instead of returning the resultant division, it returns the remainder
// So movieLength % 60 divides the movieLength into whole minutes and returns however many seconds remain

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

// It is giving the total number of minutes of the movieLength to the nearest whole number (rounded down)
// It does this by finding removing the remainder of seconds from the total movie length to give the largest
// movieLength in seconds that is still exactly divisible by 60, then dividing by 60 to give it in minutes

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

// It looks to return a string that breaks the movie length into hours, minutes and seconds that is easier to read/understand
// (at least for a human)
// a better variable name may be something like formattedTime

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

// Yes
// The code is using basic mathematical operators, it works even with extremely small or large numbers, it works with 0, it even
// works with negative numbers, although that is a little nonsensical, it may be better to return an error or undefined in that case
// For very large numbers it would return a high number of hours, which one may want to then convert to days, or have a max limit
// It all depends on what the use case for the code is
45 changes: 45 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,48 @@ console.log(`£${pounds}.${pence}`);

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

console.log(penceStringWithoutTrailingP)

// 3. const penceStringWithoutTrailingP = penceString.substring(
// initialises a string variable with value "399" by using function substring on penceString

// 4. 0,
// first argument for substring, taking part of the string starting from the first letter

// 5. penceString.length - 1
// second argument for substring, taking the string up until the 3rd letter
// done by taking the whole string length (.length) of 4 and removing 1 (this removes the 'p')

// 6. );
// closes the arguments for the function

console.log(paddedPenceNumberString)

// 8. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
// Ensures the string is at least 3 in length, and if not adds zeros to the front
// This is to ensure later calculations don't return negative values

// 9-12. const pounds = paddedPenceNumberString.substring(
// 0,
// paddedPenceNumberString.length - 2
// );
// Performing same function as lines 3 to 6, taking the padded string and removing the last two digits
// these are the digits representing pence, leaving a string that represents pounds and intialising
// that as a pounds variable of 3

// 14. const pence = paddedPenceNumberString
// intialising new pence variable

// 15. .substring(paddedPenceNumberString.length - 2)
// performing substring function on the padded string this time for pence by starting the substring
// from the penultimate (-2) character of the padded string length


// 16. .padEnd(2, "0");
// ensures the pence string is at least 2 characters long, adding a zero to the start if not
// closing this function returns 99 to the variable pence

// 18. console.log(`£${pounds}.${pence}`);
// takes the pounds and pence variables and prints them to the log as a string with a decimal divider and added pound sign
// at the start: £3.99
Loading