-
-
Notifications
You must be signed in to change notification settings - Fork 197
London | May-2025 | Reza Jahanimir | Coursework/sprint 2 #524
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 15 commits
4537db5
2c8408a
e07fba5
b5673e8
1051cc6
12ef9ff
24fbd12
3d2dea4
390e02d
6ec7ff0
8bd0b69
cac6f1a
7a30ff0
01bd659
30743d2
b405e12
7fb1e48
b4cedf8
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,13 +1,24 @@ | ||
// Predict and explain first... | ||
// =============> write your prediction here | ||
// =============> this function takes a string and capitalize the first letter of each word, | ||
// then returns the modified string. However. if the first character is a space or a number, | ||
// the function must validate this edge case before processing to avoid unexpected results. | ||
|
||
// 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; | ||
} | ||
}; | ||
|
||
// =============> write your explanation here | ||
console.log(capitalise("reza")); | ||
|
||
// =============> the parameter name and the variable are the same while causes this error | ||
// =============> write your new code here | ||
|
||
function capitalise(str) { | ||
let cap_string = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
return cap_string; | ||
}; | ||
|
||
console.log(capitalise("reza")); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,18 +3,47 @@ | |
|
||
// this function should square any number but instead we're going to get an error | ||
|
||
// =============> write your prediction of the error here | ||
// =============> the parameter can not be a number | ||
// Valid JavaScript Identifiers | ||
// Must start with a letter (a-z, A-Z), underscore (_), or dollar sign ($). | ||
// Cannot start with a number (0-9). | ||
// Can contain letters, numbers, underscores, or dollar signs after the first character. | ||
// Cannot be a reserved keyword (e.g., if, else, function, return). | ||
|
||
function square(3) { | ||
return num * num; | ||
} | ||
// function square(3) { | ||
// return num * num; | ||
// } | ||
|
||
// console.log(square(4)); | ||
|
||
// =============> write the error message here | ||
// /Users/setup/Documents/CYF/Module-Structuring-and-Testing-Data/Sprint-2/1-key-errors/2.js:13 | ||
// function square(3) { | ||
// ^ | ||
|
||
// SyntaxError: Unexpected number | ||
// at wrapSafe (node:internal/modules/cjs/loader:1662:18) | ||
// at Module._compile (node:internal/modules/cjs/loader:1704:20) | ||
// at Object..js (node:internal/modules/cjs/loader:1895:10) | ||
// at Module.load (node:internal/modules/cjs/loader:1465:32) | ||
// at Function._load (node:internal/modules/cjs/loader:1282:12) | ||
// at TracingChannel.traceSync (node:diagnostics_channel:322:14) | ||
// at wrapModuleLoad (node:internal/modules/cjs/loader:235:24) | ||
// at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:171:5) | ||
// at node:internal/main/run_main_module:36:49 | ||
|
||
// Node.js v22.16.0 | ||
|
||
// =============> explain this error message here | ||
// First Two Lines: Locate the file path and the specific line of code causing the error. | ||
// Following Lines: Display the type of error (SyntaxError) and its message. | ||
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 is a simple and straight forward explanation, nice job |
||
|
||
// Finally, correct the code to fix the problem | ||
|
||
// =============> write your new code here | ||
function square(num) { | ||
return num * num; | ||
} | ||
|
||
console.log(square(4)); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
// Predict and explain first... | ||
|
||
// =============> write your prediction here | ||
// the console.log inside the function will run but the one in global will raise the undefine error | ||
// we will get an undefine error because we are not retuning the value | ||
|
||
function multiply(a, b) { | ||
console.log(a * b); | ||
|
@@ -9,6 +11,14 @@ function multiply(a, b) { | |
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); | ||
|
||
// =============> write your explanation here | ||
// To use the final result of a function, we must return the value. Otherwise, it will raise an undefined error. | ||
|
||
// Finally, correct the code to fix the problem | ||
// =============> write your new code here | ||
function multiply(a, b) { | ||
const result = a * b; | ||
console.log(result); | ||
return result; | ||
} | ||
|
||
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. You provided the right solution here, with a simple suggestion. The |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,5 +15,9 @@ | |
// It should return their Body Mass Index to 1 decimal place | ||
|
||
function calculateBMI(weight, height) { | ||
// return the BMI of someone based off their weight and height | ||
} | ||
const height_squared = height * height; | ||
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. Simple solutions that are simple to read are always good code, which you have done here. Just want to draw your attention to some built-in methods that JavaScript has for this same thing https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/pow |
||
const bmi = height_squared / weight; | ||
return bmi.toFixed(1); | ||
} | ||
|
||
console.log(calculateBMI(83, 180)); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,10 +4,15 @@ | |
|
||
function formatAs12HourClock(time) { | ||
const hours = Number(time.slice(0, 2)); | ||
if (hours > 12) { | ||
return `${hours - 12}:00 pm`; | ||
if (hours === 0) { | ||
return "12 am"; // midnight | ||
} else if (hours === 12) { | ||
return "12 pm"; // noon | ||
} else if (hours > 12) { | ||
return `${hours - 12} pm`; | ||
} else { | ||
return `${hours} am`; | ||
} | ||
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 solution is returning You need to take a look at the original question again and see how you can do that 🙂 |
||
return `${time} am`; | ||
} | ||
|
||
const currentOutput = formatAs12HourClock("08:00"); | ||
|
@@ -23,3 +28,10 @@ console.assert( | |
currentOutput2 === targetOutput2, | ||
`current output: ${currentOutput2}, target output: ${targetOutput2}` | ||
); | ||
|
||
console.assert(formatAs12HourClock("00:00") === "12 am", "Midnight failed"); | ||
console.assert(formatAs12HourClock("01:00") === "1 am", "1am failed"); | ||
console.assert(formatAs12HourClock("08:00") === "8 am", "8am failed"); | ||
console.assert(formatAs12HourClock("12:00") === "12 pm", "Noon failed"); | ||
console.assert(formatAs12HourClock("13:00") === "1 pm", "1pm failed"); | ||
console.assert(formatAs12HourClock("23:00") === "11 pm", "11pm failed"); |
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.
Great solution on this question, but remember that if a variable will not be reassigned after it has been declared, then it should be a const, only use let if you believe that the variable will change later on