Skip to content

ITP-JAN 2025 | London | ELVIRA MLADENOVA | MODULE-STRUCTURING AND TESTING DATA | Week 4 | Sprint1 #354

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 12 commits into
base: main
Choose a base branch
from
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 updating the value (count)
//The left-hand side (count) is the variable being assigned a value,while = is the assignment operation.
//The = operator assigns the new value (1) to the count variable, replacing the old value (0).
//Now, count stores 1
3 changes: 2 additions & 1 deletion 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 = ``;
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: 5 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,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 lastDotIndex = base.lastIndexOf(".");
const dir = filePath.slice(0, lastSlashIndex);
const ext = base.slice(lastDotIndex);
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
14 changes: 14 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,17 @@ 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 represent a random integer between 1 and 100
//1. Math.random() generates a random decimal number between 0 and 1

//2. maximum - minimum + 1 calculates the numbers we want to generate.
// In this case, 100 - 1 + 1 = 100, so the range is 100.

//3. Math.floor() rounds the number down to the nearest whole number.
//This ensures we get an integer between 0 and 99

//4. + minimum
//Adding minimum (which is 1) shifts the range from [0, 99] to [1, 100].
//This means num is now a whole number between 1 and 100
5 changes: 4 additions & 1 deletion Sprint-1/2-mandatory-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
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?
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
Expand Up @@ -2,3 +2,7 @@

const age = 33;
age = age + 1;

Choose a reason for hiding this comment

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

Please remove duplicate code above which causes an error

let age = 33
age += 1
console.log(age);
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";

The code brings the error “cityOfBirth is not defined” because the cityOfBirth is used before it is defined.Line 5 should come before line 4 to have the proper result.

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

//It is not working because we can not use "slice method " with numbers only with strings and arrays.
//In this case we have number.We should turn the number to string in order to work.


//solution:
const cardNumber = 4533787178994213;
const last4Digits = cardNumber.toString().slice(-4);
console.log(last4Digits);
7 changes: 6 additions & 1 deletion Sprint-1/2-mandatory-errors/4.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
const 12HourClockTime = "20:53";
const 24hourClockTime = "08:53";
const 24hourClockTime = "08:53";

//Variable names cannot start with a number in JavaScript.And the input is not at the correct time.
//corrected:
const hourClockTime24 = "20:53";
const hourClockTime12 = "08:53";

Choose a reason for hiding this comment

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

You fixed all errors in the files but you forgot to comment out initial lines that caused error in the first place

14 changes: 14 additions & 0 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,25 @@ 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
//5 function calls:Line 4 → replaceAll(",", "")
//Line 4 → Number()
//Line 6 → replaceAll(",", "")
//Line 6 → Number()
//Line 10 → 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?
//The error is on line 5:priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));
//The function call replaceAll(",", "") is incorrect because there is an extra double quote inside replaceAll(), making it invalid syntax.
//The correct syntax is:priceAfterOneYear.replaceAll(",", "")

// c) Identify all the lines that are variable reassignment statements
//line 4:carPrice = Number(carPrice.replaceAll(",", "")); and line 5: priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));


// d) Identify all the lines that are variable declarations
// 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;

// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
//carPrice.replaceAll(",", "") -This removes all commas from the carPrice string.Number() converts string into a number.
//The purpose of Number(carPrice.replaceAll(",", "")) is to convert a string representing a price into a numerical value that can be used for calculations.
10 changes: 10 additions & 0 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,24 @@ 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?
// 6 variable declarations: Line 1,3,4,6,7,9

// b) How many function calls are there?
// 1 function call → console.log(result);

// c) Using documentation, explain what the expression movieLength % 60 represents
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators
//In this program, movieLength % 60 is used to extract the remaining seconds after calculating the total number of complete minutes in the movie.
//The expression movieLength % 60 uses the modulus operator (%) to compute the remainder when movieLength (in seconds) is divided by 60.

// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
//const totalMinutes = (movieLength - remainingSeconds) / 60;-The expression (movieLength - remainingSeconds) / 60 calculates the total number of complete minutes in the movie, excluding any leftover seconds.

// e) What do you think the variable result represents? Can you think of a better name for this variable?
//The variable result represents the formatted time of the movie in the (hours:minutes:seconds)format.Represents the total duration of the movie
//Better name: movieTime

// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
//For small and medium-sized movie lengths, the code works well- converting seconds to a properly formatted HH:MM:SS string.
//For negative movieLength values, the code could give incorrect results-If movieLength is negative (e.g., -1000), the modulus (%) operator and division will still function mathematically, but the results may not make sense in the context of time.
//If movieLength is extremely large, like movieLength = 99999999 seconds, the code will still work, but the output will be impractical
14 changes: 13 additions & 1 deletion Sprint-1/3-mandatory-interpret/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,16 @@ console.log(`£${pounds}.${pence}`);
// Try and describe the purpose / rationale behind each step

// To begin, we can start with
// 1. const penceString = "399p": initialises a string variable with the value "399p"
// 1. const penceString = "399p": initialises a string variable with the value "399p".We need to store this value to work with it in the following steps.
//3. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1);-This line removes the "p" from the penceString using the substring method.
//We want to work only with the numeric part of the string, so we remove the "p" character at the end of the string ("399" from "399p").
//8. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");-This line ensures that the string representing pence is at least 3 characters long, padding it with leading zeros if necessary.
// This step ensures that even if the number of pence is less than 100, we can still treat it as having 2 digits, like "009".
//9. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2);-This line extracts the pounds part from the paddedPenceNumberString. It takes the first part of the string before the last two digits, which represent the pence.
// We want to separate the pounds from the pence. Since we have padded the pence number to 3 digits , this operation takes everything except the last two digits,which represent the pence.
//14. const pence = paddedPenceNumberString
// .substring(paddedPenceNumberString.length - 2)
// .padEnd(2, "0");- This line extracts the pence part from the string and ensures it is exactly two digits long by adding zeros if needed.
// The substring(paddedPenceNumberString.length - 2) extracts the last two digits from the padded string. Then, .padEnd(2, "0") ensures that if the number of pence is less than 10 it will be padded to two digits.
//18. console.log(`£${pounds}.${pence}`);-This line formats the output into a readable string representing pounds and pence, then prints it to the console.
// We now have two parts of the value—pounds and pence—and we need to combine them into a string that represents the value in a human-readable format. This line creates a string in the format £X.XX where X is the respective number of pounds and pence, and prints it.