Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
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
41 changes: 38 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,39 @@
node_modules
# Generated .gitignore file for: Node, JavaScript
# Created with FOSSA's DevOps Tools

# Node
node_modules/
npm-debug.log
yarn-error.log
.env
.DS_Store
.vscode
**/.DS_Store
dist/
.cache/
coverage/

# JavaScript
# Add JavaScript-specific ignores here
package-lock.json

# Notebooks and documentation
docs/

# Common
.DS_Store
Thumbs.db
*.log
*.bak
*.tmp
*.swp
*~

# IDE and editor generated files
.idea/
*.sublime-*
.vscode/
.devcontainer/

# Local environment files
.env
.env.local
.env.*.local
3 changes: 3 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,6 @@ 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 reassigns the count variable by evaluating the expression on the right hand side of the assignment.
// The right hand side (count + 1) is evaluated first, then the = operator assigns that value to count.
Comment on lines +7 to +9
Copy link
Contributor

Choose a reason for hiding this comment

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

Operation like count = count + 1 is very common in programming, and there is a programming term describing such operation.

Can you find out what one-word programming term describes the operation on line 3?

6 changes: 2 additions & 4 deletions Sprint-1/1-key-exercises/2-initials.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,5 @@ 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 = ``;

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

let initials = `${firstName[0]}${middleName[0]}${lastName[0]}`;
console.log(initials);
6 changes: 3 additions & 3 deletions Sprint-1/1-key-exercises/3-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ 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 = filePath.slice(0, lastSlashIndex + 1);
const ext = base.slice(base.lastIndexOf("."));

// https://www.google.com/search?q=slice+mdn
// https://www.google.com/search?q=slice+mdn
38 changes: 34 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,37 @@ 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
// Breakdown of the 'num' declaration
// The expression of `num` can be broken down into several discrete steps:

// 1. `Math.random()`, this generates a random floating point number
// This number is between 0 (inclusive) and 1 (exclusive)
console.log("Math.random():", Math.random());

// 2. `(maximum - minimum + 1)` this part simple resolve to 100
console.log("maximum - minimum + 1:", maximum - minimum + 1);

// 3. By multiplying steps 1 and 2, the floating point number is scaled from 0-1 to 0-100
// This adjusted scale is defined by the `minimum` and `maximum` variables declared earlier
console.log(
"Math.random() * (maximum - minimum + 1):",
Math.random() * (maximum - minimum + 1)
);

// 4. `Math.floor()` then rounds down to the nearest whole number
// Turning the floating point number into a whole number and adjusting the range from 0-99
console.log(
"Math.floor(Math.random() * (maximum - minimum + 1)):",
Math.floor(Math.random() * (maximum - minimum + 1))
);

// 5. Lastly, `+ minimum` shifts the result up by the value of 1
// In effect, this adjusts the range from 0-99 to 1-100
console.log(
"Math.floor(Math.random() * (maximum - minimum + 1)) + minimum:",
Math.floor(Math.random() * (maximum - minimum + 1)) + minimum
);

// The final result is a random whole number between minimum (1) and maximum (100)
// Where 100 is an inclusive valid value in the range
console.log("num:", num);
8 changes: 6 additions & 2 deletions Sprint-1/2-mandatory-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
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?

// Answer: Use comments (//) to prevent execution
// SyntaxError: Unexpected identifier - plain text is not valid JavaScript
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#comments
9 changes: 8 additions & 1 deletion Sprint-1/2-mandatory-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
// trying to create an age variable and then reassign the value by 1

const age = 33;
// const age = 33;
// age = age + 1;

// TypeError: Assignment to constant variable - `const` cannot be reassigned
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const

// Solution:
let age = 33;
age = age + 1;
11 changes: 9 additions & 2 deletions 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 ?
// What's the error?

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

// ReferenceError: Cannot access 'cityOfBirth' before initialization - temporal dead zone
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const#temporal_dead_zone_tdz

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

// TypeError: cardNumber.slice is not a function - .slice() exists on strings, not numbers
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice

// Solution:
const cardNumber = 4533787178994213;
const last4Digits = String(cardNumber).slice(-4);
11 changes: 9 additions & 2 deletions Sprint-1/2-mandatory-errors/4.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
const 12HourClockTime = "20:53";
const 24hourClockTime = "08:53";
// const 12HourClockTime = "20:53";
// const 24hourClockTime = "08:53";

// SyntaxError: Invalid or unexpected token - variable names cannot start with a digit
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_types#variables

// Solution:
const twelveHourClockTime = "20:53";
const twentyFourHourClockTime = "08:53";
9 changes: 9 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,20 @@ 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
// 6 function calls: Line 4 (replaceAll, Number), Line 5 (replaceAll, 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?
// SyntaxError on line 5: missing comma between arguments "," and ""
// Fix: priceAfterOneYear.replaceAll(",", "")

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

// d) Identify all the lines that are variable declarations
// Line 1: let carPrice, Line 2: let priceAfterOneYear, Line 7: const priceDifference, Line 8: const percentageChange

// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
// Converts string with commas ("10,000") to number (10000) for mathematical calculations
// replaceAll(",", "") removes commas, Number() converts string to number
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll
14 changes: 13 additions & 1 deletion Sprint-1/3-mandatory-interpret/2-time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,26 @@ 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 declarations: movieLength, remainingSeconds, totalMinutes, remainingMinutes, totalHours, result

// 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
// % is the remainder operator - calculates remainder when dividing movieLength by 60
// Example: 8784 % 60 = 24 (seconds that don't fit into complete minutes)
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Remainder

// d) Interpret line 4, what does the expression assigned to totalMinutes mean?
// (movieLength - remainingSeconds) / 60 calculates complete minutes
// Subtracts leftover seconds (24) from total (8784), then divides by 60 to convert to minutes

// e) What do you think the variable result represents? Can you think of a better name for this variable?
// Represents movie length formatted as "hours:minutes:seconds"
// Better names: formattedTime, timeString, movieDuration, formattedDuration

// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer
// Works mathematically for positive integers but has formatting issues:
// - Single digits aren't zero-padded (shows "1:6:5" instead of "01:06:05")
// - Negative values produce incorrect results
// - Would need padStart() for proper time formatting
23 changes: 23 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,26 @@ 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 trailing "p" from "399p" to get "399"
// substring(0, 3) extracts characters from index 0 to 2
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring

// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
// Ensures string has at least 3 characters by adding leading zeros
// "399" stays "399", but "9" would become "009" (for 9p = £0.09)
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart

// 4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2);
// Extracts all except last 2 characters for pounds
// "399" → substring(0, 1) → "3" (£3)

// 5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0");
// Extracts last 2 characters for pence and pads to 2 digits
// "399" → substring(1) → "99", then padEnd(2, "0") → "99"
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padEnd

// 6. console.log(`£${pounds}.${pence}`);
// Formats and displays result using template literal: "£3.99"
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
Loading