Skip to content

Manchester | 25-ITP-May | Jennifer Isidienu | Sprint 1 | Coursework #683

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
6 changes: 6 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,9 @@ 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 the count variable by adding 1 to its current value.
// The = operator is used for assignment, meaning it takes the value on the right (count + 1)
// and assigns it to the variable on the left (count).
// The expression count + 1 calculates the new value, and the = operator updates
// the count variable with this new value.
// The final value of the count variable is now 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]}`;

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

console.log(initials); // "CKJ"
13 changes: 10 additions & 3 deletions Sprint-1/1-key-exercises/3-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,19 @@
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}`);

// 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 = ;
// Get the directory path (everything before the base)
const dir = filePath.slice(0, lastSlashIndex);

// Get the extension (everything after the last dot in the base)
const lastDotIndex = base.lastIndexOf(".");
const ext = base.slice(lastDotIndex);

console.log(`The base part of ${filePath} is ${base}`);
console.log(`The dir part of ${filePath} is ${dir}`);
console.log(`The ext part of ${filePath} is ${ext}`);

// https://www.google.com/search?q=slice+mdn
7 changes: 7 additions & 0 deletions Sprint-1/1-key-exercises/4-random.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@ const minimum = 1;
const maximum = 100;

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

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


// num is a random integer between 1 and 100
// The expression Math.random() generates a random floating-point number between 0 (inclusive) and 1 (exclusive).
// Multiplying this by (maximum - minimum + 1) scales the range to the desired size.
// Adding minimum shifts the range to start at the minimum value.
4 changes: 3 additions & 1 deletion Sprint-1/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?
We don't want the computer to run these 2 lines - how can we solve this problem?

We can use comment to ignore the two lines so the computer doesn't run them.
9 changes: 6 additions & 3 deletions Sprint-1/2-mandatory-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// trying to create an age variable and then reassign the value by 1

const age = 33;
age = age + 1;
// Trying to create an age variable and then reassign the value by 1

let age = 33; // Create a changeable variable
age = age + 1; // Increase the value by 1

console.log("New age:", age);
9 changes: 8 additions & 1 deletion Sprint-1/2-mandatory-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
// 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}`);
// console.log(`I was born in ${cityOfBirth}`);
// const cityOfBirth = "Bolton";

// The error occurs because the variable `cityOfBirth` is being used before it has been declared.
// In JavaScript, variables declared with `const` must be initialized before they can be used.
// To fix this, we need to declare the variable before using it in the console.log statement.

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

// The code will not work because `cardNumber` is a number,
// and the `slice` method is not available on numbers.
// The `slice` method is a string method, so we need to convert
// `cardNumber` to a string first.
// The prediction is correct, and the error stated:
// cardNumber.slice is not a function"
// After converting `cardNumber` to a string, the code now works correctly.
// The last4Digits variable now correctly stores the last 4 digits of cardNumber
// The Output is "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";
const twelveHourClockTime = "08:53";
const twentyFourHourClockTime = "20:53";

console.log("12-hour clock time:", twelveHourClockTime);
console.log("24-hour clock time:", twentyFourHourClockTime);
29 changes: 28 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 @@ -12,11 +12,38 @@ 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
// There are 5 function calls in this file:
// 1. `Number(carPrice.replaceAll(",", ""))` Line 4
// 2. `carPrice.replaceAll(",", ""))` Line 4
// 3. `Number(priceAfterOneYear.replaceAll(",", ""))` Line 5
// 4. `priceAfterOneYear.replaceAll(",", "")` Line 5
// 5. `console.log(...)` Line 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?
// The error is on line 5:
// `priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));`
// It occured because there is a missing comma between arguments in replaceAll()
// To fix this, change it to `replaceAll(",", "")` without the space.
// The corrected line should be:
// `priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));`


// c) Identify all the lines that are variable reassignment statements
// Line 4: `carPrice = Number(carPrice.replaceAll(",", ""));`
// Line 5: `priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));`
// Lines 4 and 5 are variable reassignment statements because they were first
// declared in lines 1 and 2.

// 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;`
// Lines 1,2, 7 and 8 are variable declarations because they introduce new variables

// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
// The expression is converting the string representation of a car price,
// which includes commas, into a number.
// It first removes the commas from the string using `replaceAll(",", "")`
// and then converts the string into a number using the `Number()` function.

26 changes: 26 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,40 @@ 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?
// There are 6 variable declarations: movieLength, remainingSeconds,
// totalMinutes, remainingMinutes, totalHours and result.

// b) How many function calls are there?
// There is 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
// The expression movieLength % 60 calculates the remainder when movieLength
// is divided by 60.
// It is used to find the remaining seconds after converting the total
// movie length from seconds to minutes.
// for instance, if movieLength is 8784 seconds,
// the expression 8784 % 60 will return 24,
// because 8784 divided by 60 is 146 with a remainder of 24.
// the expression will return 24, which means there are 24 seconds left after
// converting the total movie length to minutes

// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
// The expression assigned to totalMinutes converts the total movie length
// from seconds to minutes by removing the remaining seconds and dividing by 60.
// For example, if movieLength is 8784 seconds,
// the expression (8784 - 24) / 60 will return 146,
// which means the movie is 146 minutes long.

// e) What do you think the variable result represents? Can you think of a better name for this variable?
// The variable result represents the total movie length showing in
// "hours, minutes and seconds".
// A better name could be movieDuration.


// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
// Yes, this code will work for all values of movieLength.
// The code can handle any positive integer value for movieLength.
// It will correctly calculate the total hours, minutes, and seconds except
// for negative or decimal values.

18 changes: 18 additions & 0 deletions Sprint-1/3-mandatory-interpret/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,21 @@ console.log(`£${pounds}.${pence}`);

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

// 2. const penceStringWithoutTrailingP = penceString.substring(
// 0, penceString.length - 1): removes the trailing 'p' from the string.

// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0")
// : pads the string with leading zeros to ensure it has at least 3 characters.

// 4. const pounds = paddedPenceNumberString.substring
// (0, paddedPenceNumberString.length - 2): extracts the pounds part of the string
// by taking all characters except the last two because it represents the pence.

// 5. const pence = paddedPenceNumberString.substring
// (paddedPenceNumberString.length - 2).padEnd(2, "0"):
// extracts the pence part of the string by taking the last two characters
// and ensuring it has exactly 2 characters by padding with zeros if necessary.
Comment on lines +39 to +42
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we expect this program to work as intended for any valid penceString if we deleted .padEnd(2, "0") from the code?
In other words, do we really need .padEnd(2, "0") in this script?

Copy link
Author

Choose a reason for hiding this comment

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

I think it's needed because the program may not work as intended if we deleted. Am I correct?

Copy link
Contributor

Choose a reason for hiding this comment

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

Why don't you try executing the script without using padEnd(2, '0'), and test it with different values of penceString to validate your expectation?

Copy link
Author

Choose a reason for hiding this comment

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

Everything still works fine. It seems that .padstart(3, "0") already ensures the string has at least 3 characters. So, when we extract the last two characters for the pence part, no extra padding is needed afterward.
Thanks a lot!


// 6. console.log(`£${pounds}.${pence}`): prints the final formatted string
// in pounds and pence e.g. £3.99.
4 changes: 4 additions & 0 deletions Sprint-1/4-stretch-explore/chrome.md
Original file line number Diff line number Diff line change
Expand Up @@ -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?
It creates a popup message box in the browser that displays the message.

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 shows a popup dialog with a text input field and message. Also, the user can type something and press OK or Cancel.

What is the return value of `prompt`?
Once the user types something and clicks OK, the input text is returned as a string. If the user clicks Cancel, it returns 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 @@ -5,12 +5,22 @@ 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?
The output is:
ƒ log() { [native code] }

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

Try also entering `typeof console`
The output is:'object'

Answer the following questions:

What does `console` store?
It stores function for logging information, debugging and displaying messages in the browser's developer console.

What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean?
`console.log` means to use the log function that belongs to the console object to print messages to the console.”
`console.assert` means to use the assert method from the console object to test if something is true.
The `.` is called dot notation and it is used It is used in javascript to access properties or methods of an object.