-
-
Notifications
You must be signed in to change notification settings - Fork 195
NW7 | Jovy_So | Structuring-and-Testing-Data | WEEK 1 #65
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
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,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,8 @@ | ||
// trying to create an age variable and then reassign the value by 1 | ||
|
||
const age = 33; | ||
/* const age = 33; | ||
age = age + 1; */ | ||
|
||
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,4 +1,4 @@ | ||
const cardNumber = 4533787178994213; | ||
const cardNumber = "4533787178994213"; | ||
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. Is this a good solution do you think? What might the consequences be in changing a number to a string? |
||
const last4Digits = cardNumber.slice(-4); | ||
|
||
// The last4Digits variable should store the last 4 digits of cardNumber | ||
|
@@ -7,3 +7,6 @@ const last4Digits = cardNumber.slice(-4); | |
// 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 | ||
|
||
|
||
//Error: cardNumber.slice is not a function |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,39 @@ | ||
const 12HourClockTime = "20:53"; | ||
const 24hourClockTime = "08:53"; | ||
//const 12HourClockTime = "20:53"; | ||
//const 24hourClockTime = "08:53"; | ||
|
||
function convert12To24(time) { | ||
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. Is this what the exercise asked for? Take another look |
||
const [hours, minutes] = time.split(":"); | ||
let hour = parseInt(hours); | ||
const period = time.includes("PM") ? "PM" : "AM"; | ||
|
||
if (period === "PM" && hour < 12) { | ||
hour += 12; | ||
} else if (period === "AM" && hour === 12) { | ||
hour = 0; | ||
} | ||
|
||
return `${hour.toString().padStart(2, '0')}:${minutes}`; | ||
} | ||
|
||
function convert24To12(time) { | ||
const [hours, minutes] = time.split(":"); | ||
let hour = parseInt(hours); | ||
const period = hour >= 12 ? "PM" : "AM"; | ||
|
||
if (hour > 12) { | ||
hour -= 12; | ||
} else if (hour === 0) { | ||
hour = 12; | ||
} | ||
|
||
return `${hour}:${minutes} ${period}`; | ||
} | ||
|
||
const twelveHourClockTime = "08:53 PM"; // Example input for 12-hour format | ||
const twentyFourHourClockTime = "20:53"; // Given input for 24-hour format | ||
|
||
const convertedTo24 = convert12To24(twelveHourClockTime); | ||
const convertedTo12 = convert24To12(twentyFourHourClockTime); | ||
|
||
console.log(`12-hour to 24-hour: ${convertedTo24}`); // Output: 20:53 | ||
console.log(`24-hour to 12-hour: ${convertedTo12}`); // Output: 8:53 PM |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,18 @@ | ||
const num = 56.5678; | ||
|
||
const wholeNumberPart = Math.floor(num); | ||
const decimalPart = num - wholeNumberPart; | ||
const roundedNum = Math.round(num); | ||
|
||
console.log(wholeNumberPart); | ||
console.log(decimalPart); | ||
console.log(roundedNum); | ||
|
||
|
||
// You should look up Math functions for this exercise https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math | ||
|
||
// Create a variable called wholeNumberPart and assign to it an expression that evaluates to 56 ( the whole number part of num ) | ||
// Create a variable called decimalPart and assign to it an expression that evaluates to 0.5678 ( the decimal part of num ) | ||
// 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 | ||
// Log your variables to the console to check your answers |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,11 +6,22 @@ Open the Chrome devtools Console, type in `console.log` and then hit enter | |
|
||
What output do you get? | ||
|
||
-->Answer: ƒ log() { [native code] } | ||
|
||
Now enter just `console` in the Console, what output do you get back? | ||
|
||
-->Answer: console {debug: ƒ, error: ƒ, info: ƒ, log: ƒ, warn: ƒ, …} | ||
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. And what is this showing you? |
||
|
||
Try also entering `typeof console` | ||
|
||
-->Answer: 'object' | ||
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 conclusion can you draw from this? |
||
|
||
Answer the following questions: | ||
|
||
What does `console` store? | ||
What does the syntax `console.log` or `console.assert` mean? In particular, what does the `.` mean? | ||
|
||
-->Answer: | ||
console: This is a built-in global object in JavaScript that provides access to the browser's debugging console. It includes various methods for logging information, warnings, errors, and more. | ||
|
||
The dot (.) is used to access properties and methods of an object. In this case, it is used to access the log and assert methods of the console object. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,13 +12,21 @@ 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? | ||
/* One. movieLength | ||
|
||
// b) How many function calls are there? | ||
/* One. console.log | ||
|
||
// c) Using documentation, explain what the expression movieLength % 60 represents | ||
/* It is used to calculate the remaining seconds in the movie length (given in seconds). | ||
Here, % is the modulus operator, which calculates the remainder of two numbers when divided. | ||
|
||
// d) Interpret line 4, what does the expression assigned to totalMinutes mean? | ||
/* It is calculating the total number of minutes in the movie length, excluding the remaining seconds. | ||
|
||
// e) What do you think the variable result represents? Can you think of a better name for this variable? | ||
/* Total length of the movie. | ||
"total_length" | ||
|
||
// f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer | ||
It works for non-negative values of movieLength. | ||
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 a movie have a negative length? |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,3 +25,9 @@ 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): remove "p" | ||
// 3. const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"): Pad the pence number string | ||
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 pad mean? What is it doing? |
||
// 4. const pounds = paddedPenceNumberString.substring(0,paddedPenceNumberString.length - 2): Extract pounds | ||
// 5. const pence = paddedPenceNumberString.substring(paddedPenceNumberString.length - 2).padEnd(2, "0");: Extract pence | ||
// 6. console.log(`£${pounds}.${pence}`);: Output 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. It's logging something. Is this the same as outputting a result? |
||
|
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.
Please write in comments don't just fix the code. Thanks!