Skip to content

Commit 1899820

Browse files
author
Yonas Gebre
committed
exercise 1-complete
1 parent 719afc9 commit 1899820

12 files changed

Lines changed: 69 additions & 15 deletions

File tree

Sprint-1/1-key-exercises/1-count.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@ count = count + 1;
44

55
// Line 1 is a variable declaration, creating the count variable with an initial value of 0
66
// Describe what line 3 is doing, in particular focus on what = is doing
7+
// it updates the value of Count variable by addiing 1 to its current value.
8+
// count value hold 1
9+

Sprint-1/1-key-exercises/2-initials.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ let lastName = "Johnson";
55
// Declare a variable called initials that stores the first character of each string.
66
// This should produce the string "CKJ", but you must not write the characters C, K, or J in the code of your solution.
77

8-
let initials = ``;
8+
let initials = `${firstName[0]}${middleName[0]}${lastName[0]}`;
9+
initials = initials.toUpperCase();
10+
console.log(initials);
911

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

Sprint-1/1-key-exercises/3-paths.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ console.log(`The base part of ${filePath} is ${base}`);
1717
// Create a variable to store the dir part of the filePath variable
1818
// Create a variable to store the ext part of the variable
1919

20-
const dir = ;
21-
const ext = ;
20+
const dir =filePath.slice(0, lastSlashIndex);
21+
const ext =filePath.slice(filePath.lastIndexOf("."));
22+
console.log(`The dir part of ${filePath} is ${dir}`);
23+
console.log(`The ext part of ${filePath} is ${ext}`);
2224

2325
// https://www.google.com/search?q=slice+mdn

Sprint-1/1-key-exercises/4-random.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
const minimum = 1;
22
const maximum = 100;
33

4-
const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
4+
// const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
5+
let range = maximum - minimum + 1;
6+
let rand = Math.random() ; // generates a random number between 0 and 1 (not including 1)
7+
let rand100 = rand * range; // generates a random number between 0 and 100 (not including 100)
8+
let rand99 = Math.floor(rand100); // generates a random number between 0 and 99 (not including 100)
9+
const num = rand99 + minimum;
10+
11+
12+
13+
console.log(`The random number between 0 and 1 is ${rand}`);
14+
console.log(`The random number between 0 and 100 is ${rand100}`);
15+
console.log(`The random number mutipled with range is ${rand99}`);
16+
console.log(`The random number is ${num} between ${minimum} and ${maximum}`);
517

618
// In this exercise, you will need to work out what num represents?
719
// Try breaking down the expression and using documentation to explain what it means

Sprint-1/2-mandatory-errors/0.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
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?
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?

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// trying to create an age variable and then reassign the value by 1
22

3-
const age = 33;
3+
//const age = 33; we should change to let age = 33; to allow reassignment
4+
let age = 33;
45
age = age + 1;

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Currently trying to print the string "I was born in Bolton" but it isn't working...
2-
// what's the error ?
2+
// what's the error ? in line 4 the variable cityOfBirth is being used before it is declared. We should declare the variable before using it in the console.log statement.
33

4-
console.log(`I was born in ${cityOfBirth}`);
54
const cityOfBirth = "Bolton";
5+
console.log(`I was born in ${cityOfBirth}`);
6+

Sprint-1/2-mandatory-errors/3.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
const cardNumber = 4533787178994213;
2-
const last4Digits = cardNumber.slice(-4);
2+
//const last4Digits = cardNumber.slice(-4);
33

44
// The last4Digits variable should store the last 4 digits of cardNumber
55
// However, the code isn't working
66
// Before running the code, make and explain a prediction about why the code won't work
77
// Then run the code and see what error it gives.
88
// Consider: Why does it give this error? Is this what I predicted? If not, what's different?
99
// Then try updating the expression last4Digits is assigned to, in order to get the correct value
10+
11+
// the code is not working becuase slide method is for strings where as cardNumber is a number. We should convert the cardNumber to string before using slice method.
12+
//Synax error in line 2, but the result is not clear from the output.
13+
14+
const last4Digits = cardNumber.toString().slice(-4);
15+
console.log(`The last 4 digits of ${cardNumber} are ${last4Digits}`);

Sprint-1/2-mandatory-errors/4.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
const 12HourClockTime = "8:53pm";
2-
const 24hourClockTime = "20:53";
1+
const HourClockTime12 = "8:53pm";
2+
const hourClockTime24 = "20:53";
3+
// valid identifier don't start with a number

Sprint-1/3-mandatory-interpret/3-to-pounds.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,23 @@ const penceString = "399p";// initialises a string variable with the value "399p
33
const penceStringWithoutTrailingP = penceString.substring(
44
0,
55
penceString.length - 1
6-
);// removes the last character "p" from the penceString and stores the result in a new variable called penceStringWithoutTrailingP. The substring method is used to extract a portion of the string, starting from index 0 and ending at the second-to-last index (length - 1).
6+
);
7+
// The substring method is used to extract a portion of the string, starting from index 0 and ending at the second-to-last index (length - 1).
78

89
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
9-
//padStart method is used to add leading zeros to the penceStringWithoutTrailingP until it reaches a length of 3 characters. The result is stored in a new variable called paddedPenceNumberString.
10+
//padStart method is used to add leading zeros to the penceStringWithoutTrailingP until it reaches a length of 3 characters
11+
// The result is stored in a new variable called paddedPenceNumberString.
12+
//esure that to represent like 0.09 or 3.99
1013
const pounds = paddedPenceNumberString.substring(
1114
0,
1215
paddedPenceNumberString.length - 2
1316
);
14-
//
17+
//slice the paddedPenceNumberString until the last two characters, which represent the pounds.
1518
const pence = paddedPenceNumberString
1619
.substring(paddedPenceNumberString.length - 2)
1720
.padEnd(2, "0");
21+
//slice the last two characters of the paddedPenceNumberString, which represent the pence.
22+
// by using penEnd method esure the it is two characters long. if it is not add zeros to end.
1823

1924
console.log(${pounds}.${pence}`);
2025

@@ -26,3 +31,14 @@ console.log(`£${pounds}.${pence}`);
2631

2732
// To begin, we can start with
2833
// 1. const penceString = "399p": initialises a string variable with the value "399p"
34+
// 2. const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1):
35+
// removes the last character "p" from the penceString and stores the result in a new variable called penceStringWithoutTrailingP.
36+
// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"):
37+
// adds leading zeros to the penceStringWithoutTrailingP until it reaches a length of 3 characters.
38+
// 4. const pounds = paddedPenceNumberString.substring(0, paddedPenceNumberString.length - 2):
39+
// it like slicing , until the last two of the paddedPenceNumberString that represents the pounds.
40+
// 5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"):
41+
// takes the last two characters
42+
// the panEnd method esure the it is two characters long. if it is not add zeros to end.
43+
// 6. console.log(`£${pounds}.${pence}`):
44+
// Final result in console in the format of pounds and pence.

0 commit comments

Comments
 (0)