-
-
Notifications
You must be signed in to change notification settings - Fork 73
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
Glasgow_Class|Shreef_Ibrahim|Structuring _Testing _Datast|Week1_exercise1-update #234
base: main
Are you sure you want to change the base?
Changes from all commits
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,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? | ||
*/ | ||
|
||
// To include instructions or comments in code for human readers without executing them. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
// trying to create an age variable and then reassign the value by 1 | ||
|
||
const age = 33; | ||
/* The value of a constant cannot change through re-assignment, | ||
we can use var or let insted*/ | ||
let age = 33; | ||
age = age + 1; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,14 @@ | ||
// Currently trying to print the string "I was born in Bolton" but it isn't working... | ||
// what's the error ? | ||
/* | ||
we try to print the value of variable `cityOfBirth` before declaring it. | ||
JavaScript throws a reference error because the variable `cityOfBirth` has not been | ||
defined at that point.*/ | ||
|
||
console.log(`I was born in ${cityOfBirth}`); | ||
const cityOfBirth = "Bolton"; | ||
console.log(`I was born in ${cityOfBirth}`); | ||
const cityOfBirth = "Bolton"; | ||
|
||
// correct version | ||
// const cityOfBirth = "Bolton"; | ||
// console.log(`I was born in ${cityOfBirth}`); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,15 @@ | ||
const cardNumber = 4533787178994213; | ||
const last4Digits = cardNumber.slice(-4); | ||
const last4Digits = cardNumber.toString().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 | ||
|
||
/*because slice() is method of String not a Number. | ||
It extracts a section of a string and returns | ||
it as a new string, without modifying the original string. | ||
*/ | ||
console.log(last4Digits) // expected output 4213 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,7 @@ | ||
const 12HourClockTime = "20:53"; | ||
const 24hourClockTime = "08:53"; | ||
// const 12HourClockTime = "20:53"; | ||
// const 24hourClockTime = "08:53"; | ||
/* | ||
Variable names must begin with a letter, | ||
underscore, non-number character. Each language has its own conventions.*/ | ||
const HourClockTime12 = "08:53"; | ||
const hourClockTime24 = "20:53"; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
let count = 0; | ||
|
||
count = count + 1; | ||
count++; | ||
|
||
// 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 | ||
|
||
// eassigning the count variable to a new value by adding 1 to its current value. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,30 @@ | ||
const movieLength = 8784; // length of movie in seconds | ||
const movieLength = 999999; // length of movie in seconds | ||
|
||
const remainingSeconds = movieLength % 60; | ||
const totalMinutes = (movieLength - remainingSeconds) / 60; | ||
console.log("the total" + totalMinutes); | ||
|
||
const remainingMinutes = totalMinutes % 60; | ||
const totalHours = (totalMinutes - remainingMinutes) / 60; | ||
|
||
const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`; | ||
console.log(result); | ||
|
||
// For the piece of code above, read the code and then answer the following questions | ||
// For the piece of code above, read the ode and then answer the following questions | ||
|
||
// a) How many variable declarations are there in this program? | ||
// 6 variabls | ||
|
||
// b) How many function calls are there? | ||
|
||
// 1 | ||
// c) Using documentation, explain what the expression movieLength % 60 represents | ||
// The remainder (%) operator returns the remainder left over when one operand is divided by a second operand. It always takes the sign of the dividend.fb | ||
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 does this particular remainder represent? 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. Represents the remaining minutes that are not part of a full hour when the movie length is expressed in hours and minutes. |
||
|
||
// d) Interpret line 4, what does the expression assigned to totalMinutes mean? | ||
// calculate the total minutes by subtracting remainingSeconds from movieLength and then dividing by 60 to convert the remaining seconds into minutes | ||
|
||
// e) What do you think the variable result represents? Can you think of a better name for this variable? | ||
// its using tamplat to store variable and any tex /the better name could be movieRuntime | ||
|
||
// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer | ||
// yes will work/but with negative numbers gives unexpected resultscls |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ const pounds = paddedPenceNumberString.substring( | |
0, | ||
paddedPenceNumberString.length - 2 | ||
); | ||
3; | ||
|
||
const pence = paddedPenceNumberString | ||
.substring(paddedPenceNumberString.length - 2) | ||
|
@@ -25,3 +26,16 @@ 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); | ||
// The substring() method her returns the part of this string from the start index up to lenght-1 => last one not inclouded then | ||
// initialises a string variable assign to the result of supStr. | ||
// 3.const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); | ||
// initialises a string variable with value of the result of padStart this case it will | ||
// retun the value of three index form the started of Str =>399. | ||
// 4.const pounds = paddedPenceNumberString.substring( 0,paddedPenceNumberString.length - 2); | ||
// samilar to 2 just in this case last to index not inclouded | ||
// 5.const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0"); | ||
// this will finsh i 4 steps A/initialises a string variable/B- git the value (paddedPenceNumberString.length - 2 == 1)and take us arg | ||
// C/ calc paddedPenceNumberString.substring(1)=="99" and the resultof padEnd() = 99 asign tp the pence | ||
Comment on lines
+38
to
+39
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. yes, it will work because it formatted when added earlier padStart(3, "0"). |
||
// 5.console.log(£${pounds}.${pence}); | ||
// This line outputs the formatted price in pounds and pence 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.
To test your understanding, how would you write an expression
(without using any variable in the expression) that yields a random
integer between -5 and 3 (including -5 and 3)?
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.
Math.floor(Math.random() * (3 + 5 + 1)) - 5;