-
-
Notifications
You must be signed in to change notification settings - Fork 72
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
WM4 | Fatima Safana |Module 2/Sprint1 Exercises | Week4 #213
base: main
Are you sure you want to change the base?
Changes from all commits
3edb6aa
672cf11
54b0fa2
c3997ab
42eb38c
8b3d065
4c77ac5
82d28fb
8903a5e
c112a9a
841618b
e51addc
6635aa3
898649a
e938899
4b79d64
464ee79
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
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? |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
// trying to create an age variable and then reassign the value by 1 | ||
|
||
const age = 33; | ||
let age = 33; | ||
age = age + 1; | ||
console.log(age); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
// 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}`); | ||
const cityOfBirth = "Bolton"; | ||
console.log(`I was born in ${cityOfBirth}`); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,19 @@ | ||
const cardNumber = 4533787178994213; | ||
const cardNumber = "4533787178994213"; | ||
const last4Digits = cardNumber.slice(-4); | ||
console.log(last4Digits); | ||
|
||
function last4(num) { | ||
return String(num.slice(-4)); | ||
} | ||
|
||
console.log(last4("12237890")); | ||
|
||
// 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 | ||
//Answers: | ||
/*The code should print out 4213, because .slice() is a string type function, thus | ||
value needs to be "string" */ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
const 12HourClockTime = "20:53"; | ||
const 24hourClockTime = "08:53"; | ||
const twelveHourClockTime = "20:53"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have you also noticed the variable names do not quite match the values assigned to the variable? |
||
const twentyFourHourClockTime = "08:53"; | ||
//variable names cannot start with numbers. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,13 @@ const num = 56.5678; | |
// Create a variable called roundedNum and assign to it an expression that evaluates to 57 ( num rounded to the nearest whole number ) | ||
|
||
// Log your variables to the console to check your answers | ||
|
||
let wholeNumberPart = Math.round(num); | ||
console.log(wholeNumberPart) | ||
|
||
let decimalPart = (num - Math.floor(num)).toFixed(4); | ||
console.log(decimalPart); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you run the program, you may notice that If you were asked to print the value of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let decimalPart = (num - Math.floor(num)).toFixed(4); |
||
|
||
let roundedNum = Math.round(num); | ||
console.log(roundedNum); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,8 +11,16 @@ | |
|
||
const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt"; | ||
const lastSlashIndex = filePath.lastIndexOf("/"); | ||
console.log(lastSlashIndex); | ||
const base = filePath.slice(lastSlashIndex + 1); | ||
console.log(base); | ||
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 dirPart = filePath.substring(45, -1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Your code should be written to to work for any valid value of |
||
console.log(`The directory path of ${dirPart}`); | ||
|
||
const extPart = dirPart + "ext.txt"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The "ext part" refers to the file extension of the file represented by the file path, which is the string after the dot in the base (including the dot). So the file extension of "package.json" would be ".json". |
||
console.log(`The ext path is ${extPart}`); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,7 @@ 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 | ||
|
||
console.log(num); | ||
|
||
// The program creates a random whole number between 1 and 100, max and min inclusive. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This exercise was expecting you to break down Also, to test your understanding, how would you write an expression |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,16 +4,21 @@ const penceStringWithoutTrailingP = penceString.substring( | |
0, | ||
penceString.length - 1 | ||
); | ||
console.log(penceStringWithoutTrailingP); | ||
|
||
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); | ||
console.log(paddedPenceNumberString); | ||
|
||
const pounds = paddedPenceNumberString.substring( | ||
0, | ||
paddedPenceNumberString.length - 2 | ||
); | ||
console.log(pounds); | ||
|
||
const pence = paddedPenceNumberString | ||
.substring(paddedPenceNumberString.length - 2) | ||
.padEnd(2, "0"); | ||
console.log(pence); | ||
|
||
console.log(`£${pounds}.${pence}`); | ||
|
||
|
@@ -24,4 +29,16 @@ console.log(`£${pounds}.${pence}`); | |
// Try and describe the purpose / rationale behind each step | ||
|
||
// To begin, we can start with | ||
// 1. const penceString = "399p": initialises a string variable with the value "399p" | ||
// 1. const penceString = "399p": initializes a string variable with the value "399p" | ||
// 3. initializes a variable that removes the "p" at the end, it uses method .substring | ||
// which returns the part of the string corresponding to the index and end index | ||
//-1 excluding the p | ||
//9. uses the method padstart to add "0" to the start of string penceStringWithoutTrailingP, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After line 9, what would There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It will return 1234 |
||
// it returns the same string unchanged because the target length is equal to the current length. | ||
// this effectively multiplies any value input as penceStringWithoutTrailingP by 100 | ||
//12. uses the substring method to return the first value of the string, this is to get | ||
// the pound. it also used length -2 to make sure its in a valid range so the program | ||
// can go through the steps with any value input. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you mean by "first value of the string"? |
||
//18.the code gets the last two digits from the paddedPenceNumberString and ensures that the result | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we expect this program to work as intended even if we deleted There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no, it will not work for single digit or double digits input There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, can you find a value of |
||
//is a two-digit string by padding it with 0. | ||
//23. Print the value of pounds and pence with a period in between using template literals. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you cannot modify this statement
const cardNumber = 4533787178994213;
(that is, keep the variable's value unchanged),
how would you modify the code so that it can still extract the last 4 digits from its value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the type of variable needs to string for .slice() method to work.