-
-
Notifications
You must be signed in to change notification settings - Fork 337
London | 26-ITP-JAN | Shuheda Begum | Sprint 2 | Structuring and Testing Data #984
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
Changes from 16 commits
135ce97
4cdd2ce
0ebb92f
794f136
364c15c
c648393
6223c28
e38cd41
a6caa89
937eb3a
e6467fb
b4c367d
5ede344
8bd5328
5023abf
bc87b24
9523989
3ee00dd
4466a9b
9b72581
4e0605d
4ea2149
5c085ca
4b98073
31843b8
ece7550
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,41 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
| // I predict when this code runs, there will be a SyntaxError before the function executes. | ||
| // This is because the identifier 'str' has already been declared. | ||
|
|
||
| // call the function capitalise with a string input | ||
| // capitalise("hello"); | ||
|
|
||
|
|
||
| // interpret the error message and figure out why an error is occurring | ||
| // Uncaught SyntaxError: Unexpected identifier 'string' - this is the error message. | ||
| // This error is occuring because the variable 'str' is being redeclared within the function, which is not allowed in JavaScript. | ||
| // The duplicate use if 'str' is causing the syntax error. | ||
|
|
||
|
|
||
| //function capitalise(str) { | ||
| //let str = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| //return str; | ||
| //} | ||
|
|
||
| // The issue is variable redeclaration. | ||
| // str is the function parameter. | ||
| // let str tries to create a new variable with the same name. | ||
| // JavaScript does not allow redeclaring a variable in the same scope. | ||
| // As a result, the code throws a SyntaxError when it encounters the second 'str' declaration. | ||
|
|
||
| // New code without the error: | ||
|
|
||
| // 0.js — fully fixed version | ||
| function capitalise(str) { | ||
| let str = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| return str; | ||
| if (!str) return str; // handles empty string | ||
| return str[0].toUpperCase() + str.slice(1); | ||
| } | ||
|
|
||
| // =============> write your explanation here | ||
| // =============> write your new code here | ||
| // Test it | ||
| console.log(capitalise("hello")); // should print "Hello" | ||
| console.log(capitalise("world")); // should print "World" | ||
| console.log(capitalise("")); // should print "" | ||
|
|
||
|
|
||
|
|
||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,29 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // Why will an error occur when this program runs? | ||
| // =============> write your prediction here | ||
| // I predict an error will occur when this program runs because the variable 'decimalNumber' is already declared inside the function 'convertToPercentage' and cannot be redeclared. | ||
|
|
||
| // Try playing computer with the example to work out what is going on | ||
| // When the 'function convertToPercentage(decimalNumber)' is created, Javascript already creates a variable called decimalNumber. then, inside the function, 'const decimalNumber = 0.5' tries to create another variable with the same name. This casues a syntax error because you cannot declare two variables with the same name in the same scope. | ||
|
|
||
| function convertToPercentage(decimalNumber) { | ||
| const decimalNumber = 0.5; | ||
| const percentage = `${decimalNumber * 100}%`; | ||
| // function convertToPercentage(decimalNumber) { | ||
| // const decimalNumber = 0.5; | ||
| // const percentage = `${decimalNumber * 100}%`; | ||
|
|
||
| return percentage; | ||
| } | ||
| // return percentage; | ||
| // } | ||
|
|
||
| console.log(decimalNumber); | ||
| // console.log(decimalNumber); | ||
|
|
||
| // =============> write your explanation here | ||
| // Write your explanation here | ||
| // I redelcared the parameter decimalNumber using const. | ||
| // I tried to log decimalNumber outside its scope. | ||
| // The variables declared inside the function are not available outside unless they are returned. | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
|
|
||
| function convertToPercentage(decimalNumber) { | ||
| const percentage = `${decimalNumber * 100}%`; | ||
| return percentage; | ||
| } | ||
|
|
||
| console.log(convertToPercentage(0.5)); // "50%" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,32 @@ | ||
|
|
||
| // Predict and explain first BEFORE you run any code... | ||
| // This function should square any number but instead we're going to get an error | ||
|
|
||
| // this function should square any number but instead we're going to get an error | ||
| // Write your prediction of the error here: | ||
| // I predict that the error will be a syntax error because the parameter '3' is not a valid variable name. | ||
| // When you define a function,, the thing inside the parentahese must be a parameter name, NOT a number. | ||
|
|
||
| // =============> write your prediction of the error here | ||
| // function square(3) { | ||
| // return num * num; | ||
| // } | ||
|
|
||
| function square(3) { | ||
| return num * num; | ||
| } | ||
| // Write the error message here | ||
| // SyntaxError: Unexpected number | ||
|
|
||
| // =============> write the error message here | ||
|
|
||
| // =============> explain this error message here | ||
| // Explain the error messge here: | ||
| // When defining a function, inside the parentheses, you need to put a parameter name, which is a variable. | ||
| // In this case, '3' is not a valid parameter name because it is a number, not a variable. | ||
| // This causes a syntax error because Javascript expects a name, not a number. | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // Write your new code here | ||
|
|
||
| function square(num) { | ||
| return num * num; | ||
| } | ||
|
|
||
| console.log(square(3)); // 9 | ||
| console.log(square(5)); // 25 | ||
|
|
||
| // =============> write your new code here | ||
|
|
||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,24 @@ | ||
| // Predict and explain first... | ||
| // Predict and explain first...write your prediction here | ||
| // I predict, when the code runs, it will print 320, but it will also print "The result of multiplying 10 and 32 is undefined". | ||
|
|
||
| // =============> write your prediction here | ||
| //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 | ||
| // The function multiply(a, b) logs the result of a * b to the console but does not return a value. | ||
| // When multiply(10, 32) is called within the template literal, it logs 320 to the console but returns undefined. | ||
| // Therefore, inside the template literal, the value is undefined, which is why the final output becomes "The result of multiplying 10 and 32 is undefined". | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // Write your new code here | ||
| // To fix the problem, the function should return the result instead of logging it. | ||
|
|
||
| function multiply(a, b) { | ||
| console.log(a * b); | ||
| return a * b; | ||
| } | ||
|
|
||
| console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); | ||
|
|
||
| // =============> write your explanation here | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,23 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
| // Predict and explain first...write your prediction here | ||
| // I think the code will return undefined. | ||
|
|
||
| function sum(a, b) { | ||
| return; | ||
| a + b; | ||
| } | ||
| //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 | ||
| // When JavaScript sees the 'return' statement, it immediately stopd the function. It does NOT continue to the next line. | ||
| // So this part, 'a+b;' never runs. To return the sum of a and b, we need to put it on the same line as the return. | ||
| // Like the: 'return a + b;' | ||
|
|
||
| // =============> write your explanation here | ||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
|
|
||
| function sum(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,24 +1,43 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // Predict the output of the following code: | ||
| // =============> Write your prediction here | ||
| // Write your prediction here | ||
| // I think the output will be 'The last digit of 42 is 2', 'The last digit of 105 is 5', and 'The last digit of 806 is 6'. | ||
| // I think this will happen because the getLastDigit function is supposed to take a number, convert it to a string, and then return the last charecter of that string, which should be the last digit. | ||
|
|
||
| const num = 103; | ||
| //const num = 103; | ||
|
|
||
| function getLastDigit() { | ||
| return num.toString().slice(-1); | ||
| //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)}`); | ||
|
|
||
| // Now run the code and compare the output to your prediction | ||
| // Write the output here | ||
| // The output is: | ||
| // The last digit of 42 is 3 | ||
| // The last digit of 105 is 3 | ||
| // The last digit of 806 is 3 | ||
|
|
||
| // Explain why the output is the way it is | ||
| // write your explanation here | ||
| // The output is 3 for all numbers because the function getLastDigit is not taking any parameters. It always uses the global variable `num` which is set to 103. So it always returns the last digit of 103, which is 3. | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // Write your new code here | ||
|
|
||
| function getLastDigit(number) { | ||
| return number.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)}`); | ||
|
|
||
| // Now run the code and compare the output to your prediction | ||
| // =============> write the output here | ||
| // Explain why the output is the way it is | ||
| // =============> write your explanation here | ||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
|
|
||
| // This program should tell the user the last digit of each number. | ||
| // Explain why getLastDigit is not working properly - correct the problem | ||
| // The function did not accept a parameter. It was a fixed variable (num = 103), so it always returned the last digit of 103. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,30 @@ | |
| // Then when we call this function with the weight and height | ||
| // It should return their Body Mass Index to 1 decimal place | ||
|
|
||
| function calculateBMI(weight, height) { | ||
| //function calculateBMI(weight, height) { | ||
| // return the BMI of someone based off their weight and height | ||
| } | ||
| // } | ||
|
|
||
| // Notes: | ||
| // BMI = weight ÷ (height x height) | ||
| // eg. BMI = 70kg ÷ (1.73m x 1.73) | ||
| // BMI = 70kg ÷ 2.99 | ||
| // BMI = 23.41 | ||
| // BMI = 23.4 (to 1 decimal place) | ||
|
|
||
| // I need to: | ||
| // 1. Square the height | ||
| // 2. Divide weight by squared height | ||
| // 3. Round the result to 1 decimal place | ||
| // 4. Return the result | ||
|
|
||
| function calculateBMI(weight, height) { | ||
| const bmi = weight / (height * height); | ||
| return bmi.toFixed(1); | ||
|
Contributor
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 type of value do you expect your function to return? A number or a string? Different types of values may appear identical in the console output, but they are represented and treated differently in the program. For example, console.log(123); // Output 123
console.log("123"); // Output 123
// Treated differently in the program
let sum1 = 123 + 100; // Evaluate to 223 -- a number
let sum 2 = "123" + 100; // Evaluate to "123100" -- a string. |
||
| } | ||
|
|
||
| console.log(calculateBMI(70, 1.73)); | ||
|
|
||
|
|
||
|
|
||
| //toFixed(1) tells JavaScript to round the number to 1 decimal place. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,62 @@ | ||
| // In Sprint-1, there is a program written in interpret/to-pounds.js | ||
|
|
||
| // ORIGINAL CODE: | ||
| //const penceString = "399p"; | ||
|
|
||
| //const penceStringWithoutTrailingP = penceString.substring( | ||
| // 0, | ||
| // penceString.length - 1 | ||
| //); | ||
|
|
||
| //const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); | ||
| //const pounds = paddedPenceNumberString.substring( | ||
| // 0, | ||
| // paddedPenceNumberString.length - 2 | ||
| //); | ||
|
|
||
| //const pence = paddedPenceNumberString | ||
| //.substring(paddedPenceNumberString.length - 2) | ||
| //.padEnd(2, "0"); | ||
|
|
||
| //console.log(`£${pounds}.${pence}`); | ||
|
|
||
|
|
||
| // You will need to take this code and turn it into a reusable block of code. | ||
| // 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 | ||
| // You should call this function a number of times to check it works for different inputs. | ||
| // For example: | ||
| // toPounds("399p") should return "£3.99" | ||
| // toPounds("45p") should return "£0.45" | ||
| // toPounds("9p") should return "£0.09" | ||
| // toPounds("1200p") should return "£12.00" | ||
|
|
||
| // The current code removes the "p", makes sur ethe number has at least 3 digits, splits pounds and pences and prints it in money format. | ||
| // Right now, it only works for "399p". I need it to work for "5p" and "1234p". | ||
| // I need to turn it into a function. eg. 'function toPounds(penceString)'. penceString clearly decribes what the input is. | ||
|
|
||
| function toPounds(penceString) { | ||
| const penceStringWithoutTrailingP = penceString.substring( | ||
| 0, | ||
| penceString.length - 1 | ||
| ); | ||
|
|
||
| const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); | ||
|
|
||
| const pounds = paddedPenceNumberString.substring( | ||
| 0, | ||
| paddedPenceNumberString.length - 2 | ||
| ); | ||
|
|
||
| const pence = paddedPenceNumberString | ||
| .substring(paddedPenceNumberString.length - 2) | ||
| .padEnd(2, "0"); | ||
|
|
||
| return `£${pounds}.${pence}`; | ||
| } | ||
|
|
||
| console.log(toPounds("399p")); // £3.99 | ||
| console.log(toPounds("45p")); // £0.45 | ||
| console.log(toPounds("9p")); // £0.09 | ||
| console.log(toPounds("1200p")); // £12.00 | ||
|
|
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.
Note: Deleting the global
numis optional. If we declared a parameter that's also namednum, then within the function block, the identifiernumis resolved to the parameternum.If you are interested in the topic, you can looking up these two concepts, identifier scope and identifier resolution, in the context of JavaScript programming. ChatGPT can give a good explanation.