Skip to content

Commit 0f5f4e2

Browse files
committed
update to all files sprint-2 key-exercises, mandatory errors and mandatory-interpret folders
1 parent d28242a commit 0f5f4e2

15 files changed

Lines changed: 263 additions & 0 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
let count = 0;
2+
3+
count = count + 1;
4+
5+
// Line 1 is a variable declaration, creating the count variable with an initial value of 0
6+
// Describe what line 3 is doing, in particular focus on what = is doing
7+
8+
//Answer: The = means assignment. The value of count is 0 but it increments by 1
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const firstName = "Creola";
2+
const middleName = "Katherine";
3+
const lastName = "Johnson";
4+
// const initial = "initials";
5+
// const index = 1;
6+
7+
// console.log('The ${Initial} ${index} is ${firstName.charAt(index)}');
8+
// Declare a variable called initials that stores the first character of each string.
9+
// This should produce the string "CKJ", but you must not write the characters C, K, or J in the code of your solution.
10+
11+
const initials = firstName[0] + middleName[0] + lastName[0];
12+
console.log(initials);
13+
14+
// https://www.google.com/search?q=get+first+character+of+string+mdn
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// The diagram below shows the different names for parts of a file path on a Unix operating system
2+
3+
// ┌─────────────────────┬────────────┐
4+
// │ dir │ base │
5+
// ├──────┬ ├──────┬─────┤
6+
// │ root │ │ name │ ext │
7+
// " / home/user/dir / file .txt "
8+
// └──────┴──────────────┴──────┴─────┘
9+
10+
// (All spaces in the "" line should be ignored. They are purely for formatting.)
11+
12+
const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt";
13+
const lastSlashIndex = filePath.lastIndexOf("/");
14+
const base = filePath.slice(lastSlashIndex + 1);
15+
console.log(`The base part of ${filePath} is ${base}`);
16+
// console.log('The dir path of ${dir} is ${filePath}.${lastSlashIndex}');
17+
18+
19+
// Create a variable to store the dir part of the filePath variable
20+
// Create a variable to store the ext part of the variable
21+
22+
const dir = filePath.slice(0, lastSlashIndex);
23+
const ext = base.slice(base.lastIndexOf("."));
24+
25+
console.log(`The dir part of ${filePath} is ${dir}`);
26+
console.log(`The ext part of ${base} is ${ext}`);
27+
28+
// https://www.google.com/search?q=slice+mdn
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const minimum = 1;
2+
const maximum = 100;
3+
4+
const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
5+
// (0.68 * 100 ) + 1
6+
console.log(num)
7+
8+
// In this exercise, you will need to work out what num represents?
9+
// Try breaking down the expression and using documentation to explain what it means
10+
// It will help to think about the order in which expressions are evaluated
11+
// Try logging the value of num and running the program several times to build an idea of what the program is doing
12+
13+
// Answer: For calculations i utilised BODMAS formula solving numbers in brackets first, multiplication, subtraction and addition
14+
// I used 0.68 for math.floor(random number) + 1
15+
// Sum = 69

‎Sprint-2a/2-mandatory-errors/0.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
//This is just an instruction for the first activity - but it is just for human consumption
2+
// We don't want the computer to run these 2 lines - how can we solve this problem?
3+
4+
// I have commented out the lines. The computer removes commented lines from code compilation

‎Sprint-2a/2-mandatory-errors/1.js‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// trying to create an age variable and then reassign the value by 1
2+
3+
let age = 33;
4+
age = age + 1;
5+
6+
console.log(age)
7+
8+
// The TypeError: Assignment to constant variable implies we are trying to reassign the variable twice.
9+
// This case, I have used let instead in order to allow the variable to be reused.

‎Sprint-2a/2-mandatory-errors/2.js‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Currently trying to print the string "I was born in Bolton" but it isn't working...
2+
// what's the error ?
3+
4+
const cityOfBirth = "Bolton";
5+
console.log(`I was born in ${cityOfBirth}`);
6+
7+
8+
// The error is ReferenceError: Cannot access 'cityOfBirth' before initialization
9+
// Answer: I switched the order by declaring the const first before calling /printing

‎Sprint-2a/2-mandatory-errors/3.js‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const cardNumber = 4533787178994213;
2+
const last4Digits = cardNumber.toString().slice(-4);
3+
console.log(last4Digits)
4+
5+
6+
// The last4Digits variable should store the last 4 digits of cardNumber
7+
// However, the code isn't working
8+
// Before running the code, make and explain a prediction about why the code won't work
9+
// Then run the code and see what error it gives.
10+
// Consider: Why does it give this error? Is this what I predicted? If not, what's different?
11+
// Then try updating the expression last4Digits is assigned to, in order to get the correct value
12+
13+
//Prediction was the code would run successfully without error although with the wrong results due to absence of syntax errors in the file
14+
// Error returned: TypeError: cardNumber.slice is not a function
15+
// Lesson learnt here; slice method is only available for strings or arrays not numbers
16+
// So converted the cardNumber into a string first

‎Sprint-2a/2-mandatory-errors/4.js‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const twelveHourClockTime = "8:53pm";
2+
const twentyFourHourClockTime = "20:53";
3+
console.log(twelveHourClockTime);
4+
console.log(twentyFourHourClockTime);
5+
6+
// Error - SyntaxError: Invalid or unexpected token
7+
// lesson learnt - variables cannot start with a number
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
let carPrice = "10,000";
2+
let priceAfterOneYear = "8,543";
3+
4+
carPrice = Number(carPrice.replaceAll(",", ""));
5+
priceAfterOneYear = Number(priceAfterOneYear.replaceAll(",", ""));
6+
7+
const priceDifference = carPrice - priceAfterOneYear;
8+
const percentageChange = (priceDifference / carPrice) * 100;
9+
10+
console.log(`The percentage change is ${percentageChange}`);
11+
12+
// Read the code and then answer the questions below
13+
14+
// a) How many function calls are there in this file? Write down all the lines where a function call is made
15+
// 2
16+
//carPrice = Number(carPrice.replaceAll(",", ""));
17+
//priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ","));
18+
// 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?
19+
//b) error = SyntaxError: missing ) after argument list
20+
// (",", ",")); - added , between quoted values
21+
// c) Identify all the lines that are variable reassignment statements
22+
//carPrice = Number(carPrice.replaceAll(",", ""));
23+
//priceAfterOneYear = Number(priceAfterOneYear.replaceAll("," ","));
24+
// d) Identify all the lines that are variable declarations
25+
//let carPrice = "10,000";
26+
//let priceAfterOneYear = "8,543";
27+
//const priceDifference = carPrice - priceAfterOneYear;
28+
//const percentageChange = (priceDifference / carPrice) * 100;
29+
// e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
30+
// e) cleans the amount format by removing characters such as , and leaving only number

0 commit comments

Comments
 (0)