Repository navigation
London | 26-ITP-Sept | Mars Adesina | Sprint 3 | Javascript Fundamentals #1620
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 3 commits
b354e5e
5d55703
a70897f
027513b
2795463
1472b08
e0b2b1c
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,17 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
|
|
||
| //This code will throw an error as we haven't passed an argument into str? or str is the function parameter and variable already on line 5 so it may throw an error to declare again as a variable inside the function? | ||
| // 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; | ||
| //} | ||
| //console.log(capitalise("mars")); | ||
| //SyntaxError: Identifier 'str' has already been declared | ||
| // This code threw an error as "str" has already been defined in the function capitalise(str) so let str is causing the error as it cannot be defined twice. | ||
| function capitalise(str) { | ||
| let str = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| return str; | ||
| let result = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| return result; | ||
| } | ||
|
|
||
| // =============> write your explanation here | ||
| // =============> write your new code here | ||
| console.log(capitalise("mars")); | ||
| // In my new code i'm calling the function capitalise(str) in my code "str" is only the parameter and not the variable aswell. In the variable result it will take any argument and using the toUpperCase method itll make a copy of the the letter at index zero as a capital letter so as i passed in "mars" itll return M and store it in the variable result. The method slice will return a copy of the argument from index 1 onwards so "ars" and because it's in a template literal it will join "M" and "ars" together and this is what will get stored in result and by returning result on line 14 i'm asking the function to return what is stored in result so javascript can print it using console.log or i can use it elsewhere as i'm coding. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,24 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // Why will an error occur when this program runs? | ||
| // =============> write your prediction here | ||
| // like the original code in 0.js a syntax error will occur because decimalNumber has already been declared as the parameter in the function so it cannot be redeclared inside the function with the same name. | ||
|
|
||
| // 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); | ||
| //Because decimalNumber is declared twice it wont work but when i call the function convertToPercentage and pass in 0.5 it will get assigned to the parameter decimalNumber so i need to update the code to reflect this. | ||
|
|
||
| function convertToPercentage(decimalNumber) { | ||
| const decimalNumber = 0.5; | ||
| const percentage = `${decimalNumber * 100}%`; | ||
|
|
||
| return percentage; | ||
| } | ||
| console.log(convertToPercentage(0.5)); | ||
|
|
||
| console.log(decimalNumber); | ||
|
|
||
| // =============> write your explanation here | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
| //console output: 50% |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,23 @@ | ||
|
|
||
| // Predict and explain first BEFORE you run any code... | ||
|
|
||
| // this function should square any number but instead we're going to get an error | ||
|
|
||
| // =============> write your prediction of the error here | ||
| // I think this code will throw an error because we are passing an actual number into the parameters but javascript only allows a variable to be passed into the parameters that can be used to store information so i would need to change the number 3 into num ( and num here would act as the placeholder for any number passed in when the function gets called later) for this code to work because as it is i believe num would be undefined as it hasn't been defined anywhere. | ||
|
|
||
| function square(3) { | ||
| return num * num; | ||
| } | ||
| //function square(3) { | ||
| // return num * num; | ||
| //} | ||
|
|
||
| // =============> write the error message here | ||
| // error message: SyntaxError: Unexpected number | ||
|
|
||
| // =============> explain this error message here | ||
| // This error means javascript couldn't run the code because it doesn't accept numbers as a parameter. | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
|
|
||
| // =============> write your new code here | ||
| function square(num) { | ||
| let numResult = num * num; | ||
|
|
||
| return numResult; | ||
| } | ||
|
|
||
| console.log(square(3)); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,21 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // =============> write your prediction here | ||
| // here we are declaring a function called multiply which takes in two arguments and multiplies them but because the function doesn't return the value when we call the function it hasnt been stored anywhere for it to be called upon so i think it will return undefined in the template literal. I also think because there are two console logs one will print the value 320 and the other will print the template literal but as multiply hasn't been stored anywhere it will print undefined ? | ||
|
|
||
| function multiply(a, b) { | ||
| console.log(a * b); | ||
| } | ||
| //function multiply(a, b) { | ||
| // console.log(a * b); | ||
| //} | ||
|
|
||
| console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); | ||
| //console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); | ||
|
|
||
| // =============> write your explanation here | ||
| //320 | ||
| //The result of multiplying 10 and 32 is undefined | ||
| // the function as it is only storing the result of a and b in two console logs one asking for the total which is 320 so it prints it and the other is template literal so it prints the template literal but multiply is undefined. | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
| function multiply(a, b) { | ||
| let multiplyResult = a * b; | ||
| return multiplyResult; | ||
| } | ||
|
|
||
| console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,18 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
| // The function will not return the expected result because whilst sum takes in two arguments it cannot add a+b together because it hasn't been stored in a variable before return so when we call sum it will be undefined? | ||
|
|
||
| //function sum(a, b) { | ||
| // return; | ||
| // a + b; | ||
| //} | ||
|
|
||
| //console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); | ||
| //console output: The sum of 10 and 32 is undefined | ||
| // sum is undefined because we haven't stored the operation we want it to perform correctly in a variable so its value is undefined. | ||
| // Finally, correct the code to fix the problem | ||
|
|
||
| function sum(a, b) { | ||
| return; | ||
| a + b; | ||
| let sumResult = a + b; | ||
| return sumResult; | ||
| } | ||
|
|
||
| console.log(`The sum of 10 and 32 is ${sum(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,24 +1,31 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // Predict the output of the following code: | ||
| // =============> Write your prediction here | ||
| // here we have a constant variable called num which stores the number 103 we then have declared the function getLastDigit which has an empty parameter we are then asking it to return the value stored inside num and apply the .toString method to it which turns the number 103 into a string "103" we are then taking "103" and applying the .slice method to it which will start from position -1 and return everything after it which leaves us with the string "3" then outside of the function, the function getLastDigit is being called 3 times in template literal using console.log with the arguments 42, 105 and 806 but because the variable num = "3" it will print the template literal with the "3" in the console when javascript gets to the function call for each value, which isn't the expected result. | ||
|
|
||
| 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)}`); | ||
| //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 | ||
| // output: 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 console output prints this because the the methods have only been applied to the number stored in the num variable so javascript isn't able to access the numbers passed in when the function is called in the template literal as the function getLastDigit() has no parameter so it passes in the value stored in the num variable which is "3" and so every time the function is called "3" is returned. | ||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
| function getLastDigit(num) { | ||
| let getLastDigitResult = num.toString().slice(-1); | ||
| return getLastDigitResult; | ||
| } | ||
| 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 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,3 +14,13 @@ | |
| // 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 | ||
|
|
||
| // define function | ||
| function stringToUpperCase(str) { | ||
|
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. Your function works: it gives The name Also, JavaScript already has a method called
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.
For next time: a name like |
||
| //store string in upper snake case (calculation wont change store in const) | ||
| const stringToUpperCaseResult = str.toUpperCase().replaceAll(" ", "_"); | ||
| // return result | ||
| return stringToUpperCaseResult; | ||
| } | ||
| // call function | ||
| console.log(stringToUpperCase("welcome to cyf")); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,3 +4,27 @@ | |
| // 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 toPounds(pence) { | ||
| //Declare penceString variable | ||
| let penceString = pence; | ||
|
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. Line 10 copies Also, none of the
Author
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. Thanks Abdi! I've fixed the code and pushed the changes so hopefully all should be correct now.
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. Good, |
||
| // Store penceString value with pence on the end and make sure its at least 3 characters long add 0 at start if needed. | ||
| let penceStringWithoutTrailingP = penceString | ||
| .substring(0, penceString.length - 1) | ||
| .padStart(3, "0"); | ||
| // copy everything apart from the last two characters | ||
| let poundsResult = penceStringWithoutTrailingP.substring( | ||
| 0, | ||
| penceStringWithoutTrailingP.length - 2, | ||
| ); | ||
| // start at the last two characters and take everything from there to end and make sure that value is two characters long add 0 if needed | ||
| let penceResult = penceStringWithoutTrailingP | ||
| .substring(penceStringWithoutTrailingP.length - 2) | ||
| .padEnd(2, "0"); | ||
| // return result of poundResult and penceResult in a template string | ||
| return `£${poundsResult}.${penceResult}`; | ||
| } | ||
| //call function to see if works with different inputs | ||
| console.log(toPounds("399p")); | ||
| console.log(toPounds("1000p")); | ||
| console.log(toPounds("15000p")); | ||
Uh oh!
There was an error while loading. Please reload this page.
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.
Your fixed code works. But is a variable really what was missing? Try
return a + b;with no variable. Does it work?Now look at the original line 5. What does
return;do on its own? Does line 6 ever run?Please update line 11 with the real reason why the original code wasn't working.
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.
That's the real reason now, and
return a + b;works without a variable. Good.