-
-
Notifications
You must be signed in to change notification settings - Fork 392
West-Midlands | 26-ITP-May | Maryam Janjua | Sprint 2 | Sprint 2 Coursework #1523
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
3d1b781
c8b7d5a
c45088d
616dc24
815729e
5e3518c
698726b
5ddb233
fcd51d7
4ac532b
d50ef0b
952149c
0cfe806
d07c452
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,20 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
|
|
||
| // The Error will occur because we are trying to create a variable with same name in same scope. | ||
| // 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)}`; | ||
| str = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| return str; | ||
| } | ||
|
|
||
| let str = 'hello'; | ||
| console.log(capitalise(str)); | ||
| // =============> write your explanation here | ||
| // =============> write your new code here | ||
| //By capitilise(str)-> we have created a function with parameter str. | ||
| // Inside the function, `str` is already declared as a local variable because it is a parameter. | ||
| // We can't declare another variable with the same name using `let` in the same scope. | ||
| // It was giving an error because we were trying to declare `str` again using `let`. | ||
| // I corrected it by removing `let` because we don't need to create another variable with the same name; we just have to assign a new value to the existing `str`. | ||
| // Outside the function, I created another variable with the same name, `str`. We can do this because it is in a different scope. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,21 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // =============> write your prediction here | ||
|
|
||
| function multiply(a, b) { | ||
| // In this example function is being called but as there's no return statement so it will return undefined. | ||
| // and a *b multiplication. and the line on line 10 | ||
| /*function multiply(a, b) { | ||
| console.log(a * b); | ||
| } | ||
|
|
||
| console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); | ||
|
|
||
| */ | ||
| // =============> write your explanation here | ||
|
|
||
| // I fixed it by adding a return statement and create another variable outside of the function that stores, | ||
| //value of function. | ||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
| function multiply(a, b) { | ||
| return (a*b); | ||
| } | ||
| const result = multiply(10,32); | ||
| console.log(`The result of multiplying 10 and 32 is ${result}`); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,24 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
|
|
||
| function sum(a, b) { | ||
| //It will give an error as we have to mention what are we returning, by simply putting the return statement doesn't automatically | ||
| //return anything, except undefined, and then we are doing an addition of two number but we haven't return the result of it, | ||
| //when the function will be called in line 11, it will print undefined with the sentence mentioned in literals. | ||
| /*function sum(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)}`);*/ | ||
|
|
||
| // =============> write your explanation here | ||
| //I firstly fixed the return statement. | ||
| //outside of the function I create another variable that stores function value. | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
|
|
||
| function sum(a, b) { | ||
| return a+b; | ||
| } | ||
| const result = sum(10,32); | ||
| console.log(`The sum of 10 and 32 is ${result}`); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,4 +16,12 @@ | |
|
|
||
| function calculateBMI(weight, height) { | ||
| // return the BMI of someone based off their weight and height | ||
| } | ||
| const squareHeight = height * height; | ||
| const div = weight / 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. Please review the BMI calculation formula in the instructions above again. |
||
| const result = div.toFixed(1); | ||
| return result; | ||
| } | ||
| const height = 2.3; | ||
| const weight = 64; | ||
| const result = calculateBMI(weight, height); | ||
| console.log(result); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,3 +14,17 @@ | |
| // You will need to come up with an appropriate name for the function | ||
| // Use the MDN string documentation to help you find a solution | ||
| // This might help https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase | ||
|
|
||
| function SnakeUpperCase(str){ | ||
|
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 logic looks good, but the function name could be more descriptive. Also, you don't need to loop over every character in the string before calling https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll |
||
| var result = str.toUpperCase(); | ||
| let strLength = result.length; | ||
| for (let i=0; i<strLength; i++){ | ||
| if(result.charAt(i)==" "){ | ||
| newstr = result.replaceAll(" ", "_"); | ||
| } | ||
| } | ||
| return newstr; | ||
| } | ||
| let sentence = "hello world i am maryam"; | ||
| let result = SnakeUpperCase(sentence); | ||
| console.log(result); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,3 +4,21 @@ | |
| // You will need to declare a function called toPounds with an appropriately named parameter. | ||
|
|
||
| // You should call this function a number of times to check it works for different inputs | ||
| function ConversionPenceToPounds(penceString){ | ||
|
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. Good job turning the code into a reusable function. A few things to improve:
|
||
| const penceStringWithoutTrailingP = penceString.substring(0,penceString.length - 1); | ||
| paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); | ||
| pounds = paddedPenceNumberString.substring( 0,paddedPenceNumberString.length - 2); | ||
| paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); | ||
| pounds = paddedPenceNumberString.substring( 0,paddedPenceNumberString.length - 2); | ||
| const pence = paddedPenceNumberString | ||
| .substring(paddedPenceNumberString.length - 2) | ||
| .padEnd(2, "0"); | ||
| const result = `£${pounds}.${pence}`; | ||
| return result; | ||
| } | ||
| const pencestring1 = ConversionPenceToPounds("663p"); | ||
| const pencestring2 = ConversionPenceToPounds("98p"); | ||
| const pencestring3 = ConversionPenceToPounds("986p"); | ||
| console.log(pencestring1); | ||
| console.log(pencestring2); | ||
| console.log(pencestring3); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,3 +23,31 @@ console.assert( | |
| currentOutput2 === targetOutput2, | ||
| `current output: ${currentOutput2}, target output: ${targetOutput2}` | ||
| ); | ||
| const currentOutput3 = formatAs12HourClock("00:00"); | ||
| const targetOutput3 = "00:00 am"; | ||
| console.assert( | ||
| currentOutput3 === targetOutput3, | ||
| `current output: ${currentOutput3}, target output: ${targetOutput3}` | ||
| ); | ||
|
|
||
| const currentOutput4 = formatAs12HourClock("24:00"); | ||
| const targetOutput4 = "12:00 pm"; | ||
| console.assert( | ||
| currentOutput4 === targetOutput4, | ||
| `current output: ${currentOutput4}, target output: ${targetOutput4}` | ||
| ); | ||
|
|
||
| const currentOutput5 = formatAs12HourClock("12:00"); | ||
| const targetOutput5 = "12:00 am"; | ||
| console.assert( | ||
| currentOutput5 === targetOutput5, | ||
| `current output: ${currentOutput5}, target output: ${targetOutput}` | ||
| ); | ||
|
|
||
| const currentOutput5 = formatAs12HourClock(":00"); | ||
|
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. there is a duplicate variable declaration that will cause the tests to fail before running. |
||
| const targetOutput5 = "12:00 am"; | ||
| console.assert( | ||
| currentOutput5 === targetOutput5, | ||
| `current output: ${currentOutput5}, target output: ${targetOutput}` | ||
| ); | ||
|
|
||
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.
Good catch. However, please review the function's output, it seems to be returning the same result for every input.