Skip to content
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
//Answer:
// Line 3 takes the current value of `count`, adds 1, and assigns the result back to `count`.
// The = operator is the assignment operator.
6 changes: 4 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,9 @@ 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

// The variable 'initials' takes the first character of firstName, middleName, and lastName,
// and combines them to form a new string.
5 changes: 3 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,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 lastDotIndex = base.lastIndexOf(".");
const ext = base.slice(lastDotIndex);

// 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
Expand Up @@ -7,3 +7,9 @@ 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
//Answer:
// The variable 'num' stores a random integer between 'minimum' (1) and 'maximum' (100), inclusive.
// Math.random() generates a random number from 0 up to (but not including) 1.
// Multiplying by (maximum - minimum + 1) scales it to the desired range.
// Math.floor() rounds down to the nearest whole number.
// Adding 'minimum' shifts the range so it starts at 1 instead of 0.
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?
7 changes: 6 additions & 1 deletion Sprint-1/2-mandatory-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
// 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);

// We use 'let' to create a variable that can be reassigned.
// 'age' starts at 33, then we add 1 to it and store the result back in 'age'.
// console.log(age) outputs 34.
6 changes: 5 additions & 1 deletion Sprint-1/2-mandatory-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
// 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}`);

// The error was that the variable 'cityOfBirth' was used before it was declared.
// Using 'const', a variable must be declared before it can be accessed.
// Correct order: first declare the variable, then use it in console.log.
3 changes: 2 additions & 1 deletion Sprint-1/2-mandatory-errors/3.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
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
Expand Down
10 changes: 8 additions & 2 deletions Sprint-1/2-mandatory-errors/4.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
const 12HourClockTime = "20:53";
const 24hourClockTime = "08:53";
const hour12ClockTime = "20:53";
const hour24ClockTime = "08:53";

console.log(hour12ClockTime);
console.log(hour24ClockTime);

// Variable names cannot start with a number, so I use hour12ClockTime and hour24ClockTime.
// This way the code works and prints the correct times.
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 @@ -20,3 +20,26 @@ console.log(`The percentage change is ${percentageChange}`);
// d) Identify all the lines that are variable declarations

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


//Answer:
// a) Function calls are on these lines:
// carPrice = Number(carPrice.replaceAll(",", ""));
// priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
// console.log(`The percentage change is ${percentageChange}`);

// b) Error occurs on the line:
// priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));
// Reason: missing comma between arguments of replaceAll. Fix: replaceAll(",", "")

// c) Variable reassignment lines:
// carPrice = Number(carPrice.replaceAll(",", ""));
// priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));

// d) Variable declaration lines:
// let carPrice = "10,000";
// let priceAfterOneYear = "8,543";
// const priceDifference = carPrice - priceAfterOneYear;
// const percentageChange = (priceDifference / carPrice) * 100;

// e) Number(carPrice.replaceAll(",", "")) removes all commas from the string and converts it to a number so calculations can be done.
15 changes: 15 additions & 0 deletions Sprint-1/3-mandatory-interpret/2-time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,18 @@ console.log(result);
// e) What do you think the variable result represents? Can you think of a better name for this variable?

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

//Answer:
// a) Variable declarations:
// movieLength, remainingSeconds, totalMinutes, remainingMinutes, totalHours, result (6 in total)

// b) Function calls:
// console.log(result) (1 function call)

// c) movieLength % 60 gives the remainder when movieLength is divided by 60, representing the remaining seconds after full minutes are counted

// d) totalMinutes = (movieLength - remainingSeconds) / 60 calculates the total number of full minutes by removing leftover seconds and dividing by 60

// e) result represents the movie duration in hours:minutes:seconds format. A better name could be formattedMovieLength or movieTimeString

// f) The code works for any positive number of seconds, but single-digit minutes or seconds will not have leading zeros. To fix this, use padStart to format as hh:mm:ss
8 changes: 7 additions & 1 deletion Sprint-1/3-mandatory-interpret/3-to-pounds.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
const penceString = "399p";
// Initialize a string variable with the value "399p"

const penceStringWithoutTrailingP = penceString.substring(
0,
penceString.length - 1
);

//Remove the trailing "p" from the string to get just the number part, e.g. "399"
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
// Ensure the string has at least 3 characters by adding leading zeros if needed, e.g. "399" stays "399", "5" becomes "005"

const pounds = paddedPenceNumberString.substring(
0,
paddedPenceNumberString.length - 2
);
// Take all but the last two characters to get the pounds part, e.g. "3" from "399"

const pence = paddedPenceNumberString
.substring(paddedPenceNumberString.length - 2)
.padEnd(2, "0");
// Take the last two characters for the pence part. If less than 2 characters, add a zero at the end, e.g. "99" from "399"

console.log(`£${pounds}.${pence}`);
// Combine pounds and pence into a formatted string and print, e.g. "£3.99"

// This program takes a string representing a price in pence
// The program then builds up a string representing the price in pounds
Expand Down
9 changes: 9 additions & 0 deletions Sprint-1/4-stretch-explore/chrome.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,12 @@ Now try invoking the function `prompt` with a string input of `"What is your nam

What effect does calling the `prompt` function have?
What is the return value of `prompt`?

Answer:
Calling alert("Hello world!") shows a popup with the message "Hello world!" to the user.
The alert function does not return any value (returns undefined).
Calling prompt("What is your name?") shows a popup with a text input field asking the user for their name.
The return value of prompt is the text entered by the user, or null if the user presses Cancel.
Example:
const myName = prompt("What is your name?");
console.log(myName); // will display the name entered or null
10 changes: 10 additions & 0 deletions Sprint-1/4-stretch-explore/objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,13 @@ Answer the following questions:

What does `console` store?
What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean?


Answer:
1. console stores an object that contains methods for logging and debugging, such as log, warn, error, assert, etc.
2. typeof console returns "object", confirming that console is an object.
3. console.log or console.assert uses the dot (.) syntax to access a method (function) of the console object.
The dot means "this method belongs to this object".
4. Example:
console.log("Hello"); // calls the log method of the console object
console.assert(1 === 2, "Not equal!"); // calls the assert method of the console object
10 changes: 10 additions & 0 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,14 @@ function capitalise(str) {
}

// =============> write your explanation here
// The error occurs because the function expects a string argument.
// If no argument is passed, str is undefined, and accessing str[0]
// causes a TypeError since undefined has no properties.
// =============> write your new code here
function capitalise(str) {
if (typeof str !== "string" || str.length === 0) {
return "";
}

return `${str[0].toUpperCase()}${str.slice(1)}`;
}
15 changes: 15 additions & 0 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

// Why will an error occur when this program runs?
// =============> write your prediction here
// I predict there will be a SyntaxError or ReferenceError because
// the variable `decimalNumber` is declared twice: once as a parameter
// and once with `const` inside the function.

// Try playing computer with the example to work out what is going on

Expand All @@ -15,6 +18,18 @@ function convertToPercentage(decimalNumber) {
console.log(decimalNumber);

// =============> write your explanation here
// The error occurs because we are trying to declare a new constant `decimalNumber`
// inside the function that has the same name as the function parameter.
// In addition, `console.log(decimalNumber)` is outside the function scope,
// so decimalNumber is not defined there.

// Finally, correct the code to fix the problem
// =============> write your new code here
function convertToPercentage(decimalNumber) {
// Remove redeclaration, use the parameter directly
const percentage = `${decimalNumber * 100}%`;
return percentage;
}

// Example usage:
console.log(convertToPercentage(0.5)); // "50%"
11 changes: 11 additions & 0 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,28 @@
// this function should square any number but instead we're going to get an error

// =============> write your prediction of the error here
// I predict there will be a SyntaxError because you cannot put a number (3)
// directly in the parentheses of a function definition.
// Function parameters must be valid variable names.

function square(3) {
return num * num;
}

// =============> write the error message here
// SyntaxError: Unexpected number

// =============> explain this error message here
// The error occurs because the function parameter is incorrectly written as a literal number (3).
// JavaScript expects a variable name to use as the parameter, not a value.

// Finally, correct the code to fix the problem

// =============> write your new code here
function square(num) {
return num * num;
}

// Example usage:
console.log(square(3)); // 9

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

// =============> write your prediction here
// I predict that the output will be "The result of multiplying 10 and 32 is undefined"
// because the multiply function does not return a value, it only logs it to the console.

function multiply(a, b) {
console.log(a * b);
Expand All @@ -9,6 +11,15 @@ function multiply(a, b) {
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

// =============> write your explanation here
// The function multiply prints the product of a and b using console.log,
// but it does not return any value. In JavaScript, if a function does not
// return anything, it implicitly returns undefined. Therefore, when we
// try to use multiply(10, 32) inside the template literal, it inserts undefined.

// 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)}`); // 320
10 changes: 10 additions & 0 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Predict and explain first...
// =============> write your prediction here
// I predict that the output will be "The sum of 10 and 32 is undefined"
// because the function has a `return` statement with no value before the expression `a + b`.

function sum(a, b) {
return;
Expand All @@ -9,5 +11,13 @@ function sum(a, b) {
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

// =============> write your explanation here
// The function returns immediately at the `return;` statement without a value.
// The next line `a + b` is never executed. Therefore, the function returns undefined.

// 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)}`); // "42"
23 changes: 20 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,11 @@

// Predict the output of the following code:
// =============> Write your prediction here
// I predict that all three console.log statements will return "3"
// because the function getLastDigit always uses the constant num = 103
// and ignores any arguments passed to it.

// Original code
const num = 103;

function getLastDigit() {
Expand All @@ -15,10 +19,23 @@ 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
// Explain why the output is the way it is
// The output is:
// The last digit of 42 is 3
// The last digit of 105 is 3
// The last digit of 806 is 3

// =============> write your explanation here
// The function getLastDigit does not accept any parameter,
// so it always uses the global constant num = 103.
// Therefore, no matter what number we try to pass, it always returns '3'.
// The arguments (42, 105, 806) are ignored.

// Finally, correct the code to fix the problem
// =============> write your new code here
function getLastDigit(number) {
return number.toString().slice(-1);
}

// This program should tell the user the last digit of each number.
// Explain why getLastDigit is not working properly - correct the problem
console.log(`The last digit of 42 is ${getLastDigit(42)}`); // 2
console.log(`The last digit of 105 is ${getLastDigit(105)}`); // 5
console.log(`The last digit of 806 is ${getLastDigit(806)}`); // 6
10 changes: 8 additions & 2 deletions Sprint-2/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,11 @@
// It should return their Body Mass Index to 1 decimal place

function calculateBMI(weight, height) {
// return the BMI of someone based off their weight and height
}
const bmi = weight / (height * height);
return Number(bmi.toFixed(1));
}

// Check
console.log(calculateBMI(70, 1.73)); // 23.4
console.log(calculateBMI(80, 1.8)); // 24.7
console.log(calculateBMI(60, 1.6)); // 23.4
8 changes: 8 additions & 0 deletions Sprint-2/3-mandatory-implement/2-cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,11 @@
// You will need to come up with an appropriate name for the function
// Use the MDN string documentation to help you find a solution
// This might help https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase
function toUpperSnakeCase(str) {
return str.replace(/ /g, "_").toUpperCase();
}

// Check
console.log(toUpperSnakeCase("hello there")); // HELLO_THERE
console.log(toUpperSnakeCase("lord of the rings")); // LORD_OF_THE_RINGS
console.log(toUpperSnakeCase("my variable name")); // MY_VARIABLE_NAME
Loading