-
-
Notifications
You must be signed in to change notification settings - Fork 50
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
WM5 | Karam Ali | Module-JS1 | Week 2 #88
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,18 @@ | ||
// trying to create an age variable and then reassign the value by 1 | ||
|
||
const age = 33; | ||
// const age = 33; | ||
|
||
let age = 33; | ||
age = age + 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. well done! |
||
|
||
// The issue in this code is that you are attempting to reassign a new value to a | ||
// constant variable (age) after it has been initially assigned a value, and then reassign the value by 1 | ||
// In JavaScript, const is used to declare a variable whose value should not be reassigned after it's | ||
// initially set. Therefore, attempting to modify or reassign a const variable will result in an error. | ||
|
||
// solutions | ||
/* | ||
To resolve this issue, you should use let instead of const | ||
if you want to declare a variable that can be reassigned. | ||
*/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
// 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}`); | ||
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. Welldone |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,13 @@ | ||
const cardNumber = 4533787178994213; | ||
const last4Digits = cardNumber.slice(-4); | ||
// const last4Digits = cardNumber.slice(-4); | ||
const last4Digits = Math.floor(cardNumber % 10000); | ||
console.log(last4Digits); | ||
|
||
|
||
|
||
// The last4Digits variable should store the last 4 digits of cardNumber | ||
// However, the code isn't working | ||
// Make and explain a prediction about why the code won't work | ||
// Then try updating the expression last4Digits is assigned to, in order to get the correct value | ||
|
||
// .slice cannot slice numbers, it can only be used for strings. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
const 12HourClockTime = "20:53"; | ||
const 24hourClockTime = "08:53"; | ||
const twelveHourClockTime = "20:53"; | ||
const twentyFourHourClockTime = "08: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. Weldone |
||
console.log(twelveHourClockTime); | ||
console.log(twentyFourHourClockTime); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
// Predict and explain first... | ||
|
||
// The console.log will not return the value of what action you want for a and b to execute and it should be changed to return 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)}`); | ||
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 work |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
// Predict and explain first... | ||
// Line 4 is returning nothing because of semicolon and command is not confirmed. | ||
|
||
|
||
function sum(a, b) { | ||
return; | ||
a + b; | ||
return a + b; | ||
|
||
} | ||
|
||
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,23 @@ | ||
// Predict and explain first... | ||
// write down the error you predict will be raised | ||
// then call the function capitalise with a string input | ||
// interpret the error message and figure out why it's happening, if your prediction was wrong | ||
|
||
// str has been stored as a value inside the function capitalize, meaning that it will not work, | ||
// once a name has been used it cannot be reused as this will cause conflict. | ||
|
||
|
||
// 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; | ||
// } | ||
|
||
function capitalise(str) { | ||
let str = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
return str; | ||
return `${str[0].toUpperCase()}${str.slice(1)}`; | ||
} | ||
|
||
const capitaliseTest = capitalise("hi"); | ||
const myNameCapTest = capitalise("karam"); | ||
|
||
console.log(capitaliseTest); | ||
console.log(myNameCapTest); | ||
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. LGTM |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,11 +3,26 @@ | |
// Why will an error occur when this program runs? | ||
// Play computer with the example to work out what is going on | ||
|
||
//This will not work because in line 7 the variable decimal number declaration came after the function. It should come first then the function declaration. | ||
//Also we need to call the function in the console log for it to work. | ||
|
||
// function convertToPercentage(decimalNumber) { | ||
// const decimalNumber = 0.5; | ||
// const percentage = `${decimalNumber * 100}%`; | ||
|
||
// return percentage; | ||
// } | ||
|
||
// console.log(decimalNumber); | ||
|
||
//I have corrected the code below | ||
|
||
function convertToPercentage(decimalNumber) { | ||
const decimalNumber = 0.5; | ||
|
||
const percentage = `${decimalNumber * 100}%`; | ||
|
||
return percentage; | ||
} | ||
|
||
console.log(decimalNumber); | ||
|
||
console.log(convertToPercentage(0.7)); | ||
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 you did here is correct, however, you need to declare decimalNumber as a var and assign 0.7 to it, and then pass the "decimalNumber" into the function convertToPercentage instead of passing 0.7 straight. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,15 @@ | ||
|
||
// Predict and explain first... | ||
// The number 3 should have been declared as an argument not as a declaration. | ||
// this function should square any number but instead we're going to get an error | ||
// what is happening? How can we fix it? | ||
|
||
function square(3) { | ||
|
||
|
||
function square(num) { | ||
return num * num; | ||
} | ||
|
||
|
||
console.log(square(3)); | ||
console.log(square(2)); | ||
console.log(square(4)); | ||
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 done! |
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.
Welldone