Skip to content
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
4 changes: 4 additions & 0 deletions Sprint-2/1-key-exercises/1-count.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ 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

// "=" is an assignment operator and so what line 3 is doing,
// is the new value is being assigned to the variable "count",
// which in this case is by using an expression
4 changes: 3 additions & 1 deletion Sprint-2/1-key-exercises/2-initials.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const 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.

const initials = ``;
let initials = firstName.charAt(0) + middleName.charAt(0) + lastName.charAt(0);

// https://www.google.com/search?q=get+first+character+of+string+mdn

console.log(initials);
9 changes: 5 additions & 4 deletions Sprint-2/1-key-exercises/3-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt";
const lastSlashIndex = filePath.lastIndexOf("/");
const base = filePath.slice(lastSlashIndex + 1);
console.log(`The base part of ${filePath} is ${base}`);
//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

// https://www.google.com/search?q=slice+mdn
const lastDotIndex = filePath.lastIndexOf(".");
const ext = filePath.slice(lastDotIndex + 1);
const dir = filePath.slice(0, lastSlashIndex);
7 changes: 7 additions & 0 deletions Sprint-2/1-key-exercises/4-random.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,10 @@ 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);
// This expression uses a function that returns a random number between (min)1 and (max)100.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The range is right. The exercise also asks you to break the expression down. What does Math.random() give? What does Math.floor do to it? Which part makes the smallest value 1?

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.

Math.random() gives a decimal between 0 < 1. I multiply it by 100 to stretch it out, then Math.floor() chops off the decimals to make it a whole number. That gives me 0 to 99. The minimum at the end just adds 1 to the whole thing, so now it goes from 1 to 100 instead of 0 to 99

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Clear now, thanks.

// So every time I ran the program, it generated a different result (between 1 and 100).
// Math.random() gives a decimal between 0 < 1. I multiply it by 100 to stretch it out, then Math.floor() chops off the decimals
// to make it a whole number. That gives me 0 to 99.
// The minimum at the end just adds 1 to the whole thing, so now it goes from 1 to 100 instead of 0 to 99
6 changes: 4 additions & 2 deletions Sprint-2/2-mandatory-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
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?

// the answer: by using "//" on each line
7 changes: 6 additions & 1 deletion Sprint-2/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;
// the error is in line 4. It's a "TypeError: Assignment to constant variable", which was thrown because the value to
// a specific constant can only be assigned once (which is done in line 3 already).
// This wouldn't throw an error if instead of const, the let was used

let age = 33;
age = age + 1;
console.log(age);
6 changes: 3 additions & 3 deletions Sprint-2/2-mandatory-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Currently trying to print the string "I was born in Bolton" but it isn't working...
// what's the error ?
// an error in line 4, "ReferenceError: Cannot access 'cityOfBirth' before initialization", which proves my assumption that
// the value of cityOfBirth should have been assigned prior to trying to print the string

console.log(`I was born in ${cityOfBirth}`);
const cityOfBirth = "Bolton";
console.log(`I was born in ${cityOfBirth}`);
10 changes: 9 additions & 1 deletion Sprint-2/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);
const last4Digits = cardNumber.toString().slice(-4);
// alternatively, 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

// I thought it was something to do with the slice "-4" (but i turned out i forgot that negative indices start at -1, not 0)
// The actual error indicates that on the 3rd line there is a typeError: "cardNumber.slice is not a function"
// Checked the error reference and decided to look more closely. Noticed that the card number is used as a number,
// so it answers why the function couldn't be called - because they can be only called on Arrays and Strings.
// Therefore, I'll add parentheses to turn the card number into a string (so that the function could be called)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Your explanation of the error is good. But look at line 1. You changed cardNumber itself into a string. The exercise asks you to change the expression on line 3 instead. How can line 3 turn the number into a string?

@ausiejute ausiejute Sep 22, 2026

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.

Interesting. And yes, I need to focus more on what the exercise asks me to do. Fixed it by converting the number into string programmatically and then used the same method to extract the last 4 numbers

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The fix on line 2 is right now.

8 changes: 6 additions & 2 deletions Sprint-2/2-mandatory-errors/4.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
const 12HourClockTime = "8:53pm";
const 24hourClockTime = "20:53";
// SyntaxError: Invalid or unexpected token.
// it turns out that in JS variable names cannot begin with a number

const HourClockTime12 = "8:53pm";
const HourClockTime24 = "20:53";
console.log(HourClockTime24);
13 changes: 7 additions & 6 deletions Sprint-2/3-mandatory-interpret/1-percentage-change.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ let carPrice = "10,000";
let priceAfterOneYear = "8,543";

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

priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
console.log(carPrice);
const priceDifference = carPrice - priceAfterOneYear;
const percentageChange = (priceDifference / carPrice) * 100;

Expand All @@ -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

// 4(2), 5(2), 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?

// It was a syntax error, it can be fixed by adding the missing part (1 of 2 parentheses). Comma between 2 arguments was missing.
// c) Identify all the lines that are variable reassignment statements

// 4, 5
// d) Identify all the lines that are variable declarations

// 1, 2, 7, 8
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
// It turns/converts a string into a number by removing the comma and quotation marks (since they are non-number values)
14 changes: 9 additions & 5 deletions Sprint-2/3-mandatory-interpret/2-time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,18 @@ 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
// b) How many function calls are there?

// 1 (console.log)
// c) Using documentation, explain what the expression movieLength % 60 represents
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators

// The remainder operator calculates the remainder of movie in seconds (it does it by dividing the number by 60)
// d) Interpret line 4, what does the expression assigned to totalMinutes mean?

// it removes the leftover seconds that don't form a complete minute before dividing by 60 (conversion to minutes) in order
// to avoid a messy decimal
// e) What do you think the variable result represents? Can you think of a better name for this variable?

// It represents a total movie length, so could be named totalMovieLength
// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
// It won't work with all values. Most importantly, the value must be strictly numeric and positive.
// Examples: 59 gives 0:0:59, but a clock shows 00:00:59 , -90 gives 0:-1:-30 (time can't be negative),
// 90.5 gives 0:1:30.5 (also doesn't fit the standard time format)
15 changes: 12 additions & 3 deletions Sprint-2/3-mandatory-interpret/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ const penceString = "399p";

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

const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
const pounds = paddedPenceNumberString.substring(
0,
paddedPenceNumberString.length - 2
paddedPenceNumberString.length - 2,
);

const pence = paddedPenceNumberString
Expand All @@ -24,4 +24,13 @@ 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"
// 3-6. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1);
// : initializes a variable, removes the letter 'p'(it's still a string)
// 8. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
// Correct answer: Ensures the string is at least 3 characters long by adding leading zeros if needed. With "399", it stays "399". But if the input were "5p", this would become "005".
// 9-12. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2);
// Here the goal is to initialize a variable of Pounds (with the value of 3), by splitting it from 99, and it's done using the substring method
// 14-16. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0");
// Here the pence variable is introduced with the value of 99 and it's done by taking the 399 and removing 3 using substring and padEnd methods.
// 18. Here we join pounds and pence together and print out the result - the price.
2 changes: 2 additions & 0 deletions Sprint-2/4-stretch-explore/chrome.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ Let's try an example.
In the Chrome console, invoke the function `alert` with one argument, the string `"Hello world!"`;

What effect does calling the `alert` function have?
It is used to alert the user about something very important. One cannot access the rest of their screen until they manually turn it off (that's why it should only be used when no other way of notification would do the job).

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?
What is the return value of `prompt`?
It also pops up/overlays the screen, in this case it asks the user to input some data. The return value of the prompt is the one specified in the variable attached to it (in this case,"Diana").
4 changes: 4 additions & 0 deletions Sprint-2/4-stretch-explore/objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ In this activity, we'll explore some additional concepts that you'll encounter i
Open the Chrome devtools Console, type in `console.log` and then hit enter

What output do you get?
ƒ log() { [native code] }

Now enter just `console` in the Console, what output do you get back?
console {debug: ƒ, error: ƒ, info: ƒ, log: ƒ, warn: ƒ, …}

Try also entering `typeof console`

Answer the following questions:

What does `console` store?
It stores the things shown above: errors, warnings, messages, info
What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean?
I found that console.assert prints an error message only if a given condition is false (unlike console.log), in which case the `.` might mean the point from which they differentiate.
Loading