Skip to content

Complete sprint 1 exercises and fix errors in interpretation tasks #624

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 1 commit into
base: main
Choose a base branch
from
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
7 changes: 6 additions & 1 deletion Sprint-1/1-key-exercises/1-count.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@ let count = 0;
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 Three increases the value of count by 1.
The `=` sign here is the assignment operator, not a comparison.
It takes the value on the right-hand side (count + 1)
and stores it back into the count variable on the left.
*/
10 changes: 5 additions & 5 deletions Sprint-1/1-key-exercises/2-initials.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ let firstName = "Creola";
let middleName = "Katherine";
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 = firstName[0] + middleName[0] + lastName[0]; // Get the first letter of each name and combine them

let initials = ``;

// https://www.google.com/search?q=get+first+character+of+string+mdn
console.log(initials); // this will print "CKJ"

// we can also use firstName.charAt(0); // "C"
// but using "[]" is more easy and modern
// "+" joins characters
9 changes: 2 additions & 7 deletions Sprint-1/1-key-exercises/3-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,5 @@ const lastSlashIndex = filePath.lastIndexOf("/");
const base = filePath.slice(lastSlashIndex + 1);
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 = ;

// https://www.google.com/search?q=slice+mdn
const dir = filePath.slice(0, lastSlashIndex); // This gives us everything up to (but not including) the last /
const ext = base.slice(dotIndex); // gives ".txt"
23 changes: 19 additions & 4 deletions Sprint-1/1-key-exercises/4-random.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,22 @@ const maximum = 100;

const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;

// In this exercise, you will need to work out what num represents?
// 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
// In this code, the variable num represents a random whole number between 1 and 100, including both 1 and 100.
/*
Breakdown:
- Math.random() gives a number from 0 (inclusive) to 1 (exclusive), like 0.57
- (maximum - minimum + 1) calculates how many whole numbers we need (e.g. 100 - 1 + 1 = 100)
- Multiplying the random decimal by the range gives a number from 0 up to just below 100
- Math.floor(...) turns that into a whole number (e.g. 78.2 becomes 78)
- Adding minimum (1) shifts it into the correct final range (1–100 inclusive)
*/
/*
Order of evaluation:
1. (maximum - minimum + 1) → calculate the range (e.g., 100 - 1 + 1 = 100)
2. Math.random() → returns a decimal from 0 up to (but not including) 1
3. Multiply the random decimal by the range → gives a decimal between 0 and range
4. Math.floor(...) → rounds down to the nearest whole number
5. + minimum → shifts the value up so it starts from the minimum value

Final result: a random whole number between minimum and maximum, 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?
5 changes: 5 additions & 0 deletions 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;
age = age + 1; // this will cause an error because we can't reassign the value of a const(constant)variable

let age = 33;
age = age + 1;
console.log(age);
// when we want to reassign the variable to a new value we have to use let
7 changes: 7 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,10 @@

console.log(`I was born in ${cityOfBirth}`);
const cityOfBirth = "Bolton";
// this gives an error because the console.log comes before const or let variable and this kind of error is called "TDZ"
/*Even though you declared cityOfBirth with const, the console.log comes before that declaration. So JavaScript says:
"Hey! You’re trying to use cityOfBirth, but it hasn’t been initialized yet!" *\
// so to make it work i only need to change their position like below

const cityOfBirth = "Bolton"
console.log(`I was born in ${cityOfBirth}`)
20 changes: 14 additions & 6 deletions Sprint-1/2-mandatory-errors/3.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
const cardNumber = 4533787178994213;
const last4Digits = cardNumber.slice(-4); // this will give error as cardNumber.slice is not a function since .slice() only works with strings and arrays

//.slice() is a method that works on strings and arrays, not on numbers.

const cardNumber = "4533787178994213"; // since i changed the number to a string now .slice() will become a function and works perfectly
const last4Digits = cardNumber.slice(-4);
console.log(last4Digits); // will print the last 4 digits which are "4213"

// or i can use other options as well

const cardNumber = 4533787178994213;
const last4Digits = String(cardNumber).slice(-4);

console.log(last4Digits); // Output: "4213", this will give as the number as a string

// 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
//.slice() always return a value as a string
9 changes: 7 additions & 2 deletions 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 12HourClockTime = "20:53";// variable name can not start with a number
const 24hourClockTime = "08:53";// variable name can't start with a number

// to correct this code i have to begin the variable name with either letter or underscore character

clockTime12Hour="20:53"// even though "20:53" is not in 12 hour format our code will run as long as the variable is defined
clockTime24Hour="08:53"
24 changes: 24 additions & 0 deletions Sprint-1/3-mandatory-interpret/1-percentage-change.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,34 @@ 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 in the above code and we can find them in
- line 4: Number(carPrice.replaceAll(",", "")
- line 5: Number(priceAfterOneYear.replaceAll("," "")
- line 10:console.log(`The percentage change is ${percentageChange}`) *\

// 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?
/* line 5: priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));
^^^
SyntaxError: missing ) after argument list *\

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

/* line 4 and line 5
carPrice = Number(carPrice.replaceAll(",", ""));
priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));*\



// d) Identify all the lines that are variable declarations
/* Line 1,2,7 and 8 basically any line with let and const *\

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

/* carPrice.replaceAll(",","") this will replace "," with this "" which is removing the comma so in our code "10,000" will be replaced by "10000"
this is because we cant do math with a string with a comma so we need to remove the comma before doing the math. so after removing the comma from
the string we left with Number(), this function will convert the string into number so that we can do our math.
IN SUMMARY
Number("10,000") will actually return NaN (Not a Number)
❗ Because the comma confuses it

So you must remove the comma first, then convert to number. *\
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 @@ -13,13 +13,28 @@ console.log(result);

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

// there are 6 variable declaration are there,anything that starts with let and const considered as a variable declaration

// b) How many function calls are there?

// there is only one function call, which is console.log(). a function has to be like this functionName()

// c) Using documentation, explain what the expression movieLength % 60 represents

// The remainder (%) operator returns the remainder left over when one operand is divided by a second operand. It always takes the sign of the dividend.
// movieLength % 60 means How many seconds are left after dividing movieLength by 60?"
// movieLength % 60 gives you the remaining seconds after converting as many seconds as possible into whole minutes.

// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators

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

// it means take the number of full seconds that can be evenly split into minutes, and convert that to minutes.

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

// it represents a time format like this HH:MM:SS and a better name that suits the variable is timeFormat

// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
// Yes, the code will work for all non-negative integers,i tried using different values starting from 0,1 amd going upto million and the code works
// The code correctly takes a number of seconds (movieLength) and converts it into a time format: hours, minutes, and seconds — then displays it as a string like H:M:S."
34 changes: 33 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,36 @@ 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": initializes a string variable with the value "399p"

// 2. const penceStringWithoutTrailingP= Removes the trailing "p" character so we are left with just the numeric part: "399"
// to do a step by step breakdown: .substring(start,End), which "0" represents the start index "0" and penceString.length-1 is a common way to get the index of the last character in any string.
// penceString.length - 1 → length of "399p" is 4, so 4 - 1 = 3
// so now .substring(start,End) extracts characters from index 0 to index 3 (not including 3).
// "399p".substring(0, 3) // returns "399", since counting starts from index 0

// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
// .padStart(3, "0") ensures the string is at least 3 characters long and If it's shorter (like "5" or "99"), it pads it from the start with zeros.
// example "5"= 005 or "88"= 088 it makes sure the characters are always three by adding zro when its shorter
// in this case "399" stays "399"

// 4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2);
// Gets all but the last two digits
// paddedPenceNumberString = "399"-length is 3, so 3 - 2 = 1
// substring(0, 1) means get characters from index 0 up to index 1 (not including 1)
// "399".substring(0, 1) → "3" its purpose is to extract the pound from the string like this 399p= £3.99

// 5.const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length-2).padEnd(2,"0")
// (paddedPenceNumberString.length - 2) takes the last two digits of the string "399"- 3-2=1
// .substring(1): this is saying give me the part of the string starting at this index, and go to the end
// so in "399" index 1 is 9 so we will get 99, which gives us index 1 to the end
// .padEnd(2, "0"): Makes sure the result has at least 2 digits, padding with "0" if it’s too short like "5"=05,makes it two digit
//"399" → last two digits → "99" → no padding needed → stays "99"
//Extracts the pence part of the string — always exactly 2 digits to match £x.xx format.

// 6. console.log(£${pounds}.${pence});
// Combines the two parts i have built:pounds and pence then uses template literal syntax to insert the variables into a string
// and finally prints it like this £3.99

// Overall Purpose of the Code:
// This code takes a string like "399p" (meaning 399 pence) and formats it as a proper British currency string like £3.99.It handles other cases too, like "5p" → £0.05 or "99p" → £0.99, thanks to the padding.
3 changes: 3 additions & 0 deletions age.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
let age= 33
age = age + 1
console.log(age)
3 changes: 3 additions & 0 deletions cardNumber.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const cardNumber ="4533787178994213"
const last4Digits = cardNumber.slice(-4);
console.log(last4Digits)
4 changes: 4 additions & 0 deletions cardNumber2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const cardNumber = 4533787178994213;
const last4Digits = String(cardNumber).slice(-4);

console.log(last4Digits)
2 changes: 2 additions & 0 deletions cityOfBirth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const cityOfBirth = "Bolton"
console.log(`I was born in ${cityOfBirth}`)
10 changes: 10 additions & 0 deletions percentage-change.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
let carPrice = "10,000";
let priceAfterOneYear = "8,543";

carPrice = Number(carPrice.replaceAll(",", ""));
priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));

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

console.log(`The percentage change is ${percentageChange}`);
5 changes: 5 additions & 0 deletions randomNumber.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const minimum = 20;
const maximum = 50;

const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
console.log(num)
10 changes: 10 additions & 0 deletions timeFormat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const movieLength = 1000000000000000000000; // 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);