Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 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
5 changes: 5 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,8 @@ 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 a new value to the 'count' variable.
// In JavaScript, '=' is the assignment operator, not a mathematical equals sign.
// It first evaluates the expression on the right side (count + 1, which is 0 + 1),
// and then assigns that result (1) back to the variable on the left side, updating its value.
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

7 changes: 4 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,8 @@ 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(filePath.lastIndexOf("."));
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
8 changes: 8 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,11 @@ 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);
// Answer: The variable `num` represents a random whole number between 1 and 100 (inclusive).
// Breakdown of the expression:
// 1. Math.random() generates a random decimal number between 0 (inclusive) and 1 (exclusive).
// 2. (maximum - minimum + 1) calculates the total number of possible integers in our range (which is 100).
// 3. Multiplying them scales the random decimal to be between 0 and 99.999...
// 4. Math.floor() rounds that decimal down to the nearest whole number (getting an integer from 0 to 99).
// 5. Finally, '+ minimum' shifts the starting point up by 1, resulting in a final integer from 1 to 100.
9 changes: 7 additions & 2 deletions Sprint-1/2-mandatory-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
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?

// Explanation:
// Running this without comments gives a "SyntaxError".
// This occurs because the computer tries to read plain English sentences as JavaScript code, which it cannot understand.
// Adding '//' turns them into comments that the computer ignores.
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;
let age = 33;
age = age + 1;

// Explanation:
// The error is "TypeError: Assignment to constant variable."
// This occurs because we used 'const' to declare the 'age' variable. 'const' creates a constant whose value cannot be reassigned after its initial creation.
7 changes: 5 additions & 2 deletions Sprint-1/2-mandatory-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// 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}`);

// Explanation:
// The error is "ReferenceError: Cannot access 'cityOfBirth' before initialization."
// JavaScript executes code from top to bottom. We tried to print 'cityOfBirth' before we actually declared and initialized it on the next line.
8 changes: 6 additions & 2 deletions Sprint-1/2-mandatory-errors/3.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
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

// Explanation:
// The error is "TypeError: cardNumber.slice is not a function."
// This occurs because the original cardNumber was a Number type. The '.slice()' method only works on Strings and Arrays, not on pure numbers.
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 time12Hour = "20:53";
const time24Hour = "08:53";

console.log(time12Hour);
console.log(time24Hour);

// Explanation:
// The error is a "SyntaxError".
// This occurs because it violates JavaScript naming conventions: variable names cannot start with a number.
9 changes: 5 additions & 4 deletions Sprint-1/3-mandatory-interpret/1-percentage-change.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ 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

// Answer: There are 5 function calls in total. They are located on lines 4, 5, and 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?

// Answer: The error comes from line 4. Since JavaScript executes from top to bottom, it crashes on line 4 because we are trying to reassign a new value to `carPrice`, which was originally declared as a `const`. To fix it, change `const` to `let` on lines 1 and 2.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was originally declared as let and not const. Your previous solution that states the error is from line 5 is correct. What was wrong was your reason. So check line 5 again to determine the reason for the error.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, I corrected it. Please have another look.

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

// Answer: Lines 4 and 5.
// d) Identify all the lines that are variable declarations

// Answer: Lines 1, 2, 7, and 8.
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
// Answer: It removes the comma from the string using `replaceAll()`, and then converts that cleaned string into a number type using `Number()`. The purpose is to convert a formatted text string into a valid mathematical number so we can do calculations with it later.
11 changes: 6 additions & 5 deletions Sprint-1/3-mandatory-interpret/2-time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ 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?

// Answer: There are 6 variable declarations. (They are: movieLength, remainingSeconds, totalMinutes, remainingMinutes, totalHours, and result).
// b) How many function calls are there?

// Answer: There is exactly 1 function call (console.log 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

// Answer: The `%` symbol is the remainder (modulo) operator. `movieLength % 60` calculates the leftover seconds that do not fit into a full 60-second minute.
// d) Interpret line 4, what does the expression assigned to totalMinutes mean?

// Answer: It subtracts the leftover seconds from the total seconds to get a number cleanly divisible by 60, then divides by 60 to find out exactly how many full minutes are in the movie.
// e) What do you think the variable result represents? Can you think of a better name for this variable?

// Answer: `result` represents the final formatted time string (Hours:Minutes:Seconds). A better, more descriptive name could be `formattedTime` or `movieDurationString`.
// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
// Answer: It works for positive whole numbers. However, it won't format single-digit numbers perfectly (e.g., it prints 2:5:9 instead of 02:05:09). Also, it might produce weird results or decimals if we input negative numbers or fractional seconds.
16 changes: 16 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,19 @@ 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 = ...
// Removes the last character (the "p") from the string using substring(), leaving just the numbers.

// 3. const paddedPenceNumberString = ...
// Uses padStart(3, "0") to make sure the string is at least 3 characters long by adding "0"s to the front.

// 4. const pounds = ...
// Uses substring() to extract all characters EXCEPT the last two. This isolates the pound value.

// 5. const pence = ...
// Extracts exactly the last two characters for the pence value using substring(),
// and uses padEnd() as a safety measure to ensure it's exactly 2 digits long.

// 6. console.log(...)
// Uses template literals to piece the pounds and pence back together with a "£" and "." symbol, printing "£3.99".
11 changes: 8 additions & 3 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
// Predict and explain first...
// =============> write your prediction here

// It will crash with a SyntaxError. We can't use 'let' to declare 'str' because 'str' is already taken by the function parameter.
// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring

/* I commented this out so it doesn't crash:
function capitalise(str) {
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
}

*/
// =============> write your explanation here
//The error confirms "str has already been declared". Function parameters act as local variables, creating a naming conflict with `let str`.
// =============> write your new code here
function capitalise(str) {
return `${str[0].toUpperCase()}${str.slice(1)}`;
}
console.log(capitalise("hello"));
12 changes: 8 additions & 4 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

// Why will an error occur when this program runs?
// =============> write your prediction here

// It will crash for two reasons. 1. Trying to log a local variable 'decimalNumber' outside its function. 2. Trying to re-declare the parameter 'decimalNumber' using 'const' inside the function.
// Try playing computer with the example to work out what is going on

/* Commented out broken code:
function convertToPercentage(decimalNumber) {
const decimalNumber = 0.5;
const percentage = `${decimalNumber * 100}%`;
Expand All @@ -13,8 +13,12 @@ function convertToPercentage(decimalNumber) {
}

console.log(decimalNumber);

*/
// =============> write your explanation here

// 'decimalNumber' is scoped to the function, so console.log outside can't see it (ReferenceError). Inside, re-declaring the parameter with 'const' causes a SyntaxError. Also, the function was never actually called.
// Finally, correct the code to fix the problem
// =============> write your new code here
function convertToPercentage(decimalNumber) {
return `${decimalNumber * 100}%`;
}
console.log(convertToPercentage(0.5));
15 changes: 10 additions & 5 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@
// this function should square any number but instead we're going to get an error

// =============> write your prediction of the error here

// Syntax error. We put a hardcoded number '3' where a parameter name (like 'num') should be.
/* Commented out the broken code so it doesn't crash:
function square(3) {
return num * num;
}

*/
// =============> write the error message here

// SyntaxError: Unexpected number
// =============> explain this error message here

// Function definitions require variable names as parameters to act as placeholders, not literal values.
// Finally, correct the code to fix the problem

// =============> write your new code here

function square(num) {
return num * num;
}
console.log(square(3));
console.log(square(4));

11 changes: 8 additions & 3 deletions Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
// Predict and explain first...

// =============> write your prediction here

//It will print "320", followed by "The result... is undefined". The function lacks a return value.
/* Commented out the broken code:
function multiply(a, b) {
console.log(a * b);
}

console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

*/
// =============> write your explanation here

// `console.log` just prints to the screen. Without a `return` statement, the function evaluates to `undefined`, which gets injected into the template literal.
// Finally, correct the code to fix the problem
// =============> write your new code here
function multiply(a, b) {
return a * b;
}
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
10 changes: 8 additions & 2 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
// Predict and explain first...
// =============> write your prediction here

// It will print "undefined". The 'return;' statement stops the function immediately before it even looks at 'a + b'.
/* Commented out the broken code:
function sum(a, b) {
return;
a + b;
}

console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

*/
// =============> write your explanation here
// The 'return' keyword instantly exits the function. Since nothing is attached to it, it returns 'undefined'. The 'a + b;' line is dead code that never gets executed.
// Finally, correct the code to fix the problem
// =============> write your new code here
function sum(a, b) {
return a + b;
}
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
16 changes: 13 additions & 3 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

// Predict the output of the following code:
// =============> Write your prediction here

// It will always print "3" for every log. The function ignores the numbers we pass in and blindly uses the global 'num' (103).
/* Commented out the broken code:
const num = 103;

function getLastDigit() {
Expand All @@ -12,13 +13,22 @@ function getLastDigit() {
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
console.log(`The last digit of 806 is ${getLastDigit(806)}`);

*/
// Now run the code and compare the output to your prediction
// =============> write the output here
// The last digit of 42 is 3
// The last digit of 105 is 3
// The last digit of 806 is 3
// Explain why the output is the way it is
// =============> write your explanation here
// The function lacks a parameter in its definition. Therefore, it ignores the arguments (42, 105, 806) and always falls back to the globally defined 'num' which is 103.
// Finally, correct the code to fix the problem
// =============> write your new code here

function getLastDigit(number) {
return number.toString().slice(-1);
}
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
console.log(`The last digit of 806 is ${getLastDigit(806)}`);
// This program should tell the user the last digit of each number.
// Explain why getLastDigit is not working properly - correct the problem
Loading