-
-
Notifications
You must be signed in to change notification settings - Fork 164
London| Emmanuel Gessessew | Module-Structuring-and-Testing-Data/ sprint 2 | WEEK 2 #130
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
46cc0f6
cefeea6
7b970c1
2500cf3
3ca4eca
9fdeb38
cf6eacf
e1421fd
412c625
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,3 @@ | ||
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? | ||
// comments |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,10 @@ | ||
// trying to create an age variable and then reassign the value by 1 | ||
|
||
const age = 33; | ||
age = age + 1; | ||
// const age = 33; | ||
// age = age + 1; | ||
|
||
|
||
// the problem is the variable age is declared as const which mean we can not change it to fix this we change it from const to let | ||
|
||
let age = 33; | ||
age = age +1; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
// 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}`); | ||
// console.log(`I was born in ${cityOfBirth}`); | ||
// const cityOfBirth = "Bolton"; | ||
|
||
// the problem is we cannot access city of birth before initialization, here is the correct one | ||
|
||
const cityOfBirth = "Bolton"; | ||
console.log(`I was born in ${cityOfBirth}`); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,17 @@ | ||
const cardNumber = 4533787178994213; | ||
const last4Digits = cardNumber.slice(-4); | ||
// const cardNumber = 4533787178994213; | ||
// const last4Digits = cardNumber.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 | ||
|
||
|
||
// the code provided will produce an error cause the .slice() will only generate strings and arrays while the cardNumber is a number. | ||
// to correct this i will introduce the .toString() to change it to a string, | ||
|
||
const cardNumber = 4533787178994213; | ||
const last4Digits = cardNumber.toString().slice(-4); | ||
console.log(last4Digits); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,12 @@ | ||
const 12HourClockTime = "20:53"; | ||
const 24hourClockTime = "08:53"; | ||
// const 12HourClockTime = "20:53"; | ||
// const 24hourClockTime = "08:53"; | ||
|
||
// here the problem is a variable name in java script can not start with a number and also the reading in time 20:53 is in the 24 hr clock time | ||
|
||
|
||
const twelveHourClockTime = "08:53"; | ||
const twentyFourHourClockTime = "20:53"; | ||
|
||
console.log(twelveHourClockTime); | ||
console.log(twentyFourHourClockTime); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,16 @@ | ||
// Predict and explain first... | ||
|
||
// function multiply(a, b) { | ||
// console.log(a * b); | ||
// } | ||
|
||
// console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); | ||
|
||
// // correction | ||
// the multiply code will not work cause the return is not defined. replace return (a*b) instead of console.log(a*b). | ||
function multiply(a, b) { | ||
console.log(a * b); | ||
return a * b; | ||
} | ||
|
||
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,18 @@ | ||
// Predict and explain first... | ||
|
||
// function sum(a, b) { | ||
|
||
// return; | ||
// a + b; | ||
// } | ||
|
||
// console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); | ||
|
||
// correction | ||
// the sum(10,32) in the console.log will give an undefined result. a + b should be on the same line of code with return. | ||
|
||
function sum(a, b) { | ||
return; | ||
a + b; | ||
return a + b; | ||
} | ||
|
||
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); | ||
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,27 @@ | ||
// Predict and explain first... | ||
|
||
const num = 103; | ||
// const num = 103; | ||
|
||
function getLastDigit() { | ||
// function getLastDigit() { | ||
// return num.toString().slice(-1); | ||
// } | ||
|
||
// console.log(`The last digit of 42 is ${getLastDigit(42)}`); | ||
// console.log(`The last digit of 105 is ${getLastDigit(105)}`); | ||
// console.log(`The last digit of 806 is ${getLastDigit(806)}`); | ||
|
||
// This program should tell the user the last digit of each number. | ||
// Explain why getLastDigit is not working properly - correct the problem | ||
|
||
// correction | ||
// when the num is defined in the above code it uses the const it should use the let, since getLastDigit is not a constant number. | ||
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. Hmm, I don't think let or const is salient here. Do you think you need to assign 103 to num? Why would you need to do this? 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. how about this, function getLastDigit(num) { |
||
// aan also getLastDigit in the function should have a parameter (num), else it will only give the last digit of the hardcoded num which is 103. | ||
let num = 103; | ||
|
||
function getLastDigit(num) { | ||
return num.toString().slice(-1); | ||
} | ||
|
||
console.log(`The last digit of 42 is ${getLastDigit(42)}`); | ||
console.log(`The last digit of 105 is ${getLastDigit(105)}`); | ||
console.log(`The last digit of 806 is ${getLastDigit(806)}`); | ||
|
||
// This program should tell the user the last digit of each number. | ||
// Explain why getLastDigit is not working properly - correct the problem | ||
console.log(`The last digit of 806 is ${getLastDigit(806)}`); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,17 @@ | |
// call the function capitalise with a string input | ||
// interpret the error message and figure out why an error is occurring | ||
|
||
// function capitalise(str) { | ||
// let str = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
// return str; | ||
// } | ||
|
||
//correction | ||
// the problem is the capitalise function is that it is re declaring the str parameter inside the function using let. | ||
// we canot do that cause the str is already a defined function. | ||
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 not a defined function. It is defined, but what is it, exactly? Try to be precise here in your explanation. 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. function capitalize(str) { |
||
|
||
function capitalise(str) { | ||
let str = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
str = `${str[0].toUpperCase()}${str.slice(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. Do you need to assign this to str? Have a think 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. i don't need to assign the capitalized string back to str. Instead, function capitalise(str) { |
||
return str; | ||
} | ||
console.log(capitalise("emmanuel")) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,11 +3,24 @@ | |
// Why will an error occur when this program runs? | ||
// Try playing computer with the example to work out what is going on | ||
|
||
// function convertToPercentage(decimalNumber) { | ||
// const decimalNumber = 0.5; | ||
// const percentage = `${decimalNumber * 100}%`; | ||
|
||
// return percentage; | ||
// } | ||
|
||
// console.log(decimalNumber); | ||
|
||
//correction | ||
// the problem in the above code is that it redeclaring the decimalNumber with const. | ||
// and the The variable decimalNumber is only available inside the function calling it outside will result in error | ||
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 kind of error? 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 error will be when u encounter if you try to access decimalNumber outside the function is a ReferenceError |
||
|
||
function convertToPercentage(decimalNumber) { | ||
const decimalNumber = 0.5; | ||
|
||
const percentage = `${decimalNumber * 100}%`; | ||
|
||
return percentage; | ||
} | ||
|
||
console.log(decimalNumber); | ||
console.log(convertToPercentage(0.5)); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,8 +3,15 @@ | |
|
||
// this function should square any number but instead we're going to get an error | ||
|
||
function square(3) { | ||
// function square(3) { | ||
// return num * num; | ||
// } | ||
|
||
// correction | ||
// the problem in the above code is it didnt defined the num as a parameter of square. | ||
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 3 ever be a parameter in JavaScript? What kind of error did this code produce? |
||
|
||
function square(num) { | ||
return num * num; | ||
} | ||
|
||
|
||
console.log(square(3)) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,3 +22,5 @@ console.assert( | |
currentOutput2 === targetOutput2, | ||
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 happened here? Do you need help with this one? |
||
`current output: ${currentOutput2}, target output: ${targetOutput2}` | ||
); | ||
|
||
// input 1, |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,22 @@ | |
|
||
// You will need to come up with an appropriate name for the function | ||
// Use the string documentation to help you find a solution | ||
|
||
|
||
//solution | ||
|
||
function toUpperSnakeCase(input) { | ||
|
||
const withUnderscores = input.replace(/ /g, "_"); | ||
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. Nice! |
||
|
||
|
||
const upperSnakeCase = withUnderscores.toUpperCase(); | ||
|
||
|
||
return upperSnakeCase; | ||
} | ||
|
||
|
||
console.log(toUpperSnakeCase("hello there")); | ||
console.log(toUpperSnakeCase("lord of the rings")); | ||
|
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.
It would be very beneficial for your learning to formally write out your prediction before changing the code.
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.
Thanks for the suggestion! I'll make sure to write out my predictions before making any changes in the future to better understand the code and improve my learning process.