Skip to content

ITP JAN 2025 LONDON| ELFREDAH KEVIN-ALERECHI |Structuring-and-Testing-Data| SPRINT 1 #445

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
15 changes: 15 additions & 0 deletions Sprint-1/1-key-exercises/1-count.js
Original file line number Diff line number Diff line change
@@ -4,3 +4,18 @@ 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 updating the value of count. Therefore, line 3 is just increasing the value of count by 1.


//RESPONSE VELOW
Line 3: count = count + 1;
This line updates the value of the count variable by increasing it by 1.

The = in this line is the assignment operator, not a comparison operator. It assigns the value on the right-hand side to the variable on the left-hand side.

So count + 1 is evaluated first (which gives 1, since count was 0).

Then the result (1) is assigned back to the variable count.

So overall, Line 3 means:
“Take the current value of count, add 1 to it, and store the new value back in count.”
8 changes: 4 additions & 4 deletions Sprint-1/1-key-exercises/2-initials.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
let firstName = "Creola";
let middleName = "Katherine";
let lastName = "Johnson";
let firstName = "Elfredah";
let middleName = "Kevin";
let lastName = "Alerechi";

// 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]";
Copy link

Choose a reason for hiding this comment

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

With this statement, the value assigned to initials is not "EKA" (the first character of each string).


// 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
@@ -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 = ;
//SOLUTIONS BELOW

const dir ="filePath.slice(0, lastSlashIndex)";
Copy link

Choose a reason for hiding this comment

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

This statement does not assign the dir part to dir.

const ext = base.split(".").pop();

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

//solutions below:

const minimum = 1;
const maximum = 100;

const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
What is num?
num ends up being a random integer between minimum (1) and maximum (100) — inclusive of both 1 and 100.

//Let's break down the expression:

Math.floor(Math.random() * (maximum - minimum + 1)) + minimum
Math.random()

This generates a random decimal number between 0 (inclusive) and 1 (exclusive).

Example output: 0.472, 0.9999, 0.015

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

I'm multiplying the random decimal by the size of the range.

maximum - minimum + 1 ensures the range is inclusive.

In this case: 100 - 1 + 1 = 100

//So now you're getting a number between 0 and just under 100.

Math.floor(...)

//This rounds down the result to the nearest whole number.

//So now you get an integer from 0 to 99.

+ minimum

Finally, you add the minimum value (1), shifting the range from:

0–99 → 1–100

//Final result:
//So, num will always be a random whole number between 1 and 100, inclusive.
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: 4 additions & 0 deletions Sprint-1/2-mandatory-errors/1.js
Original file line number Diff line number Diff line change
@@ -2,3 +2,7 @@

const age = 33;
age = age + 1;

let age = 33; // Using let to declare a variable that can be reassigned
age = age + 4; // Now I am reassigning the value of age
console.log(age); // This will print 37 to the console
8 changes: 6 additions & 2 deletions 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";
//The error occurs because you are trying to use the variable cityOfBirth before it is defined.
// In JavaScript, the line const cityOfBirth = "Bolton";
// needs to run before you try to use cityOfBirth in the console.log() statement. See the corrected code below:

const cityOfBirth = "Bolton"; // First, define the variable
console.log(`I was born in ${cityOfBirth}`); // Then, use it
15 changes: 14 additions & 1 deletion Sprint-1/2-mandatory-errors/3.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
const cardNumber = 4533787178994213;
const last4Digits = cardNumber.slice(-4);
const last4Digits = cardNumber.slice(-4); // This will throw an error

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

//SOLUTION BELOW

//The code will not work because cardNumber is a number, and the .slice() method is used for strings, not numbers.
// The .slice() method cannot be used directly on a number, which will lead to an error.
//TypeError: cardNumber.slice is not a function.
//This happens because .slice() is a method that only works on strings and arrays, but cardNumber is a number.
// You can't call .slice() on a number.
//To fix this, we need to convert the number to a string before using .slice() to extract the last 4 digits:

const cardNumber = 4533787178994213;
const last4Digits = String(cardNumber).slice(-4); // Convert to string first
console.log(last4Digits); // This will output "4213"
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";
//CORRECTED CODE BLOW
const twelveHourClockTime = "20:53"; // Use letters instead of starting with a number
const twentyFourHourClockTime = "08:53"; // Use letters instead of starting with a number


58 changes: 58 additions & 0 deletions Sprint-1/3-mandatory-interpret/1-percentage-change.js
Original file line number Diff line number Diff line change
@@ -20,3 +20,61 @@ 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?

//There are two function calls in the file. They are:

//a. carPrice.replaceAll(",", "") – This calls the replaceAll() method to remove commas from the carPrice string.
//priceAfterOneYear.replaceAll(",", "") – This also calls the replaceAll() method to remove commas from the priceAfterOneYear string.

//FULL RESPONSE BELOW:

// --- CODE STARTS HERE ---

let carPrice = "10,000";
let priceAfterOneYear = "8,543";

// Removing commas and converting to numbers
carPrice = Number(carPrice.replaceAll(",", ""));
priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));

const priceDifference = carPrice - priceAfterOneYear;
const percentageChange = (priceDifference / carPrice) * 100;

console.log(`The percentage change is ${percentageChange}`);
Copy link

Choose a reason for hiding this comment

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

% could be added here

Copy link
Author

Choose a reason for hiding this comment

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

Thanks. % has been added to console.log(The percentage change is ${percentageChange}%)


// --- QUESTIONS & ANSWERS ---

// 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:
// Line 4: carPrice.replaceAll(",", "")
// Line 4: Number(...)
// Line 5: priceAfterOneYear.replaceAll(",", "")
// Line 5: Number(...)
// Line 8: 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?
// Answer:
// The original error was in line 5 due to a missing comma in replaceAll.
// Incorrect: replaceAll("," "")
// Corrected: replaceAll(",", "")
// The error occurred because the function call had incorrect syntax, missing a second argument.
// Fix: Add the missing comma in replaceAll

// c) Identify all the lines that are variable reassignment statements
// Answer:
// Line 4: carPrice is reassigned with a numeric version
// Line 5: priceAfterOneYear is reassigned with a numeric version

// d) Identify all the lines that are variable declarations
// Answer:
// Line 1: let carPrice = "10,000";
// Line 2: let priceAfterOneYear = "8,543";
// Line 6: const priceDifference = ...
// Line 7: const percentageChange = ...

// e) Describe what the expression Number(carPrice.replaceAll(",", "")) is doing - what is the purpose of this expression?
// Answer:
// First, carPrice.replaceAll(",", "") removes commas from the string, turning "10,000" into "10000"
// Then, Number(...) converts the string "10000" into the number 10000
// The purpose is to convert a formatted price string into a number so that mathematical operations can be performed.
57 changes: 57 additions & 0 deletions Sprint-1/3-mandatory-interpret/2-time-format.js
Original file line number Diff line number Diff line change
@@ -23,3 +23,60 @@ 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


// --- CODE STARTS HERE ---

const movieLength = 8784; // length of movie in seconds

const remainingSeconds = movieLength % 60;
const totalMinutes = (movieLength - remainingSeconds) / 60;

const remainingMinutes = totalMinutes % 60;
const totalHours = (totalMinutes - remainingMinutes) / 60;

const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`;
console.log(result);

// --- QUESTIONS & ANSWERS ---

// a) How many variable declarations are there in this program?
// Answer:
// There are 5 variable declarations:
// 1. movieLength
// 2. remainingSeconds
// 3. totalMinutes
// 4. remainingMinutes
// 5. totalHours
// 6. result
// (So actually: **6** variable declarations using `const`)

// b) How many function calls are there?
// Answer:
// Only 1 function call: `console.log(result);`

// c) Using documentation, explain what the expression movieLength % 60 represents
// Answer:
// The `%` operator returns the **remainder** after division.
// So `movieLength % 60` gives the number of **remaining seconds** after dividing the total seconds by 60.
// In this case, it isolates the seconds that don’t fit into full minutes.
// Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#remainder

// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
// Answer:
// `(movieLength - remainingSeconds) / 60` calculates how many **full minutes** are in the movie.
// It first subtracts the leftover seconds, then divides by 60 to convert the rest into minutes.

// e) What do you think the variable result represents? Can you think of a better name for this variable?
// Answer:
// `result` represents the **converted movie length in HH:MM:SS format** (hours:minutes:seconds).
// A better name would be: `formattedTime`, `formattedDuration`, or `movieTimeFormatted` — more descriptive and readable.

// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
// Answer:
// Yes, this code will work for all **non-negative integer** values of `movieLength` (in seconds).
// However:
// - If movieLength is **less than 60**, it will display `0:0:seconds`
// - It doesn’t add leading zeroes (e.g., `1:3:4` instead of `01:03:04`), so formatting could be improved for better readability.
// - If `movieLength` is a **negative number**, it still runs but the output won't make logical sense for a duration.
// So it works functionally, but you may want to add validation and zero-padding for a more polished version.
84 changes: 84 additions & 0 deletions Sprint-1/3-mandatory-interpret/3-to-pounds.js
Original file line number Diff line number Diff line change
@@ -25,3 +25,87 @@ console.log(`£${pounds}.${pence}`);

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



//SOLUTIONS HERE


// 1. const penceString = "399p": initialises a string variable with the value "399p"
const penceString = "399p";

// 2. Removes the trailing "p" by taking a substring from index 0 to the second-to-last character.
const penceStringWithoutTrailingP = penceString.substring(
0,
penceString.length - 1
);

// 3. Pads the numeric string to ensure it's at least 3 digits (e.g., "7" becomes "007").
// This makes it easier to extract pounds and pence.
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");

// 4. Extracts the pound portion by taking all but the last two digits.
const pounds = paddedPenceNumberString.substring(
0,
paddedPenceNumberString.length - 2
);

// 5. Extracts the pence portion by taking the last two digits and padding with "0" if needed.
const pence = paddedPenceNumberString
.substring(paddedPenceNumberString.length - 2)
.padEnd(2, "0");

// 6. Logs the formatted result in the form £x.xx
console.log(`£${pounds}.${pence}`);

// --- QUESTIONS & DETAILED RESPONSES ---

// Q: What does the program do?
// A: This program takes a price in pence as a string with a "p" suffix (e.g., "399p")
// and converts it to a properly formatted British pound value (e.g., £3.99).

// Q: Step-by-step breakdown of what each line does?
//
// 1. const penceString = "399p":
// - Initializes the input value with a string representing the number of pence.
//
// 2. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1):
// - Removes the trailing 'p' to isolate the numeric part (e.g., "399p" → "399").
//
// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"):
// - Ensures the number is at least 3 characters long by padding with zeros if necessary.
// - Useful for values like "7" (→ "007") to ensure valid pounds/pence splitting.
//
// 4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2):
// - Extracts the pound portion by slicing off the last two digits.
//
// 5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"):
// - Extracts the pence portion (last 2 digits), padding if needed to ensure two-digit formatting.
//
// 6. console.log(`£${pounds}.${pence}`):
// - Outputs the formatted currency in pounds and pence.

// Q: Will this code work for all values?
// A: It works for most valid pence strings (like "5p", "99p", "7p", "399p"). However:
// - It does not validate whether the input ends with "p".
// - It assumes the value is in the correct format.
// - You could improve it by adding input validation and supporting values without "p".

// --- OPTIONAL ENHANCEMENT ---
// To convert this into a reusable function that accepts any pence string:
function formatPenceToPounds(penceInput) {
if (!penceInput.endsWith("p")) {
return "Invalid input format";
}

const raw = penceInput.slice(0, -1);
const padded = raw.padStart(3, "0");
const pounds = padded.substring(0, padded.length - 2);
const pence = padded.substring(padded.length - 2).padEnd(2, "0");
return `£${pounds}.${pence}`;
}

console.log(formatPenceToPounds("5p")); // Output: £0.05
console.log(formatPenceToPounds("99p")); // Output: £0.99
console.log(formatPenceToPounds("125p")); // Output: £1.25
console.log(formatPenceToPounds("7")); // Output: Invalid input format
4 changes: 4 additions & 0 deletions Sprint-1/4-stretch-explore/chrome.md
Original file line number Diff line number Diff line change
@@ -11,8 +11,12 @@ In the Chrome console,
invoke the function `alert` with an input string of `"Hello world!"`;

What effect does calling the `alert` function have?
//RESPONSE
It says undefined
Comment on lines 13 to +15
Copy link

Choose a reason for hiding this comment

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

Didn't you see anything happened in the browser when your call alert("Hello world!") in the browser JS console?


Now try invoking the function `prompt` with a string input of `"What is your name?"` - store the return value of your call to `prompt` in an variable called `myName`.

What effect does calling the `prompt` function have?
//It popup a field to add my name
What is the return value of `prompt`?
It says undefined
Comment on lines 21 to +22
Copy link

Choose a reason for hiding this comment

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

prompt() should not have returned undefined. You can look up the function at MDN to find out what its return value is.

Loading
Oops, something went wrong.