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-1/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
//ANS:
// Line 3 is incrementing the variable.
// It evaluates the expression on the right using the variable's current state,
// then uses the = operator to store that new result back into the variable, effectively updating it.
4 changes: 3 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,9 @@ 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); // Output: "CKJ"

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

17 changes: 15 additions & 2 deletions Sprint-1/1-key-exercises/3-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,26 @@

const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt";
const lastSlashIndex = filePath.lastIndexOf("/");
// 1. Get the 'base' (file.txt)
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 = ;
//const dir = ;
//const ext = ;

// 2. Get the 'dir' (/Users/mitch/cyf/Module-JS1/week-1/interpret)
// We slice from the start up to the last slash
const dir = filePath.slice(0, lastSlashIndex);

// 3. Get the 'ext' (.txt)
// We find the last dot and slice from there to the end
const lastDotIndex = filePath.lastIndexOf(".");
const ext = filePath.slice(lastDotIndex);

console.log(`Dir: ${dir}`);
console.log(`Ext: ${ext}`);

// https://www.google.com/search?q=slice+mdn
8 changes: 8 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,11 @@ 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

//ANS:
//num represents a randomly generated integer between a specified minimum (1) and maximum (100), inclusive.
//Math.random()This function returns a floating-point (decimal) number that is greater than or equal to $0$ but strictly less than $1$
//(maximum - minimum + 1) represents the total number of possible integers within the range (from 1 to 100)
// Math.random() * 100: multiplication of random decimal by range.
// Adding the minimum value (1) to the result "shifts" the entire range upward so it doesn't start at zero.
//Math.floor() rounds a number down to the nearest whole integer.
7 changes: 5 additions & 2 deletions 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?
//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?

//ANS:
//Use two forward slashes //
10 changes: 10 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,13 @@

const age = 33;
age = age + 1;


//ANS:
//Use let
//let allows you to create a variable whose value can be reassigned as many times as you like.

let age = 33; // Use 'let' instead of 'const'
age = age + 1;

console.log(age);
8 changes: 8 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,11 @@

console.log(`I was born in ${cityOfBirth}`);
const cityOfBirth = "Bolton";


//ANS:
//"Cannot access 'cityOfBirth' before initialization."
//SOlution:

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

//ANS: code will throw an error
// 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?

//ANS:
//The .slice() method is a String (and Array) method. cardNumber is defined as a Number. Numbers in JavaScript do not have a .slice() property

// Then try updating the expression last4Digits is assigned to, in order to get the correct value


//ANS:
const cardNumber = 4533787178994213;

// Convert to string first, then slice
const last4Digits = cardNumber.toString().slice(-4);

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

//FIx:
const time12Hour = "08:53 PM";
const time24Hour = "20:53";
31 changes: 26 additions & 5 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,32 @@ 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

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

//ANS:There are 6 function calls in total.
carPrice.replaceAll(",", "")(Line 4)
Number(...)(Line 4)
priceAfterOneYear.replaceAll("," "")(Line 5)
Number(...)(Line 5)
console.log(...)(Line 10)
percentageChange inside the template literal(the engine calls a string conversion here).
// 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?
//ANS: error here -> priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ""));
//FIX:
priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
// c) Identify all the lines that are variable reassignment statements

//ANS:
carPrice = Number(carPrice.replaceAll(",", ""));
priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
// d) Identify all the lines that are variable declarations

//ANS:
let carPrice = "10,000";
let priceAfterOneYear = "8,543";
const priceDifference = carPrice - priceAfterOneYear;
const percentageChange = (priceDifference / carPrice) * 100;
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
//ANS:
//This expression is performing Data Cleaning and Type Conversion.
carPrice.replaceAll(",", "") // This finds every comma in the string "10,000" and removes it, resulting in the string "10000".

Number(...) //This takes that cleaned string and converts it into an actual Number data type.

//The Purpose: Computers cannot perform mathematical calculations(like division or subtraction) on strings that contain formatting characters like commas.
25 changes: 25 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,39 @@ 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?
//ANS: There are 6 variable declarations.
//movieLength
//remainingSeconds
//totalMinutes
//remainingMinutes
//totalHours
//result

// b) How many function calls are there?
//ANS: One explicit 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

//ANS: The % symbol is the Remainder Operator, according to the documentation, it returns the remainder left over when one operand is divided by a second operand.

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

//ANS:
//This expression calculates the total number of whole minutes contained within the movieLength.
//(movieLength - remainingSeconds): This subtracts the "leftover" seconds so that the number is perfectly divisible by 60.

// The / 60: This divides that number by 60 to convert the unit from seconds to minutes.

// e) What do you think the variable result represents? Can you think of a better name for this variable?
//ANS: The result variable represents the duration of the movie formatted as a timestamp (HH:MM:SS).
//Better names for this variable:
//formattedDuration
//MovieTimestamp
//timeString

// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
//ANS: Mathematically: It will work for any positive number.
//It does not handle padding. If remainingSeconds is 5, the output will look like 2:26:5. Standard timestamps expect two digits (e.g., 2:26:05).
//If movieLength is a negative number or a non-number (like a string), the math will result in NaN (Not a Number) or unexpected logic.
26 changes: 26 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,29 @@ 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);
//Purpose: To strip away the "p" character so we are left with only the digits.
//Rationale: You cannot perform formatting or math on the letter "p".substring(0, length - 1) says "start at the beginning and take everything except the very last character."Result: "399"

//3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
//Purpose: To ensure the string is at least 3 characters long by adding a leading "0" if necessary.
//Rationale: This is a "safety" step.If the input was "5p", the previous step would give us "5".By padding it to "005", we ensure there are always enough digits to represent both pounds and pence(e.g., £0.05).
//Result: "399"(No change here because it's already 3 digits, but "5" would become "005").

//4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2);
//Purpose: To extract the digits that represent the "pounds" portion of the price.
//Rationale: In a pence - to - pounds conversion, everything except the last two digits represents the pounds.By taking the substring from the start up to the "length minus 2," we grab that "left side" of the decimal point.
// Result: "3"

//5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0");
//Purpose: To extract the last two digits(the pence) and ensure they are formatted correctly.
// Rationale: * .substring(length - 2) grabs the final two characters.

// .padEnd(2, "0") is a double - safety measure; if for some reason we only had one digit, it would add a zero to the end(e.g., turning "5" into "50").
// Result: "99"

//6. console.log("£${pounds}.${pence}");
//Purpose: To combine the processed pieces into a human - readable currency format.
// Rationale: This uses a template literal to inject the pounds and pence variables into a string, separated by a decimal point and prefixed with a "£" symbol.
// Final Output: "£3.99"
8 changes: 7 additions & 1 deletion Sprint-1/4-stretch-explore/chrome.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@ In the Chrome console,
invoke the function `alert` with an input string of `"Hello world!"`;

What effect does calling the `alert` function have?

## Got "chrome://new-tab-page says" popup
## Hello world!
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?
## Got "chrome://new-tab-page says" popup
## Got "What is your name?"
## Got a box for input
What is the return value of `prompt`?
## promt: ƒ prompt() { [native code] }
## myName: Ayo
11 changes: 8 additions & 3 deletions Sprint-1/4-stretch-explore/objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@ 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`

## 'object'
Answer the following questions:

What does `console` store?
## Objects
## console {debug: ƒ, error: ƒ, info: ƒ, log: ƒ, warn: ƒ, …}
What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean?
## console.log - Prints the text to the Console as a log message
## console.assert() writes an error message to the console if the assertion is false
## The dot(.) allows the access of the log method inside the console object
Loading