generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 336
London | 26-Jan-ITP | Shuheda Begum | Sprint 2 | Coursework #1070
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
Open
codebyshay
wants to merge
10
commits into
CodeYourFuture:main
Choose a base branch
from
codebyshay:Sprint-2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+542
−213
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
91f41d8
Sprint one task complete
codebyshay be5c268
Completed all task required for sprint 2
codebyshay 211f3eb
Description of increment
codebyshay 17b2307
3.js restored
codebyshay 8578bb7
Update 3.js
codebyshay b20e658
Update 3.js
codebyshay 70c46af
testing commit
codebyshay 7b1d8e1
Merge branch 'main' of https://github.com/codebyshay/Module-Structuri…
codebyshay c55aab8
Added terminating the function
codebyshay 8e61326
Added jest tests
codebyshay File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| 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? | ||
| // The forward slash is used to indicate a comment in JavaScript, so the computer will ignore these lines when running the program. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,10 @@ | ||
| // trying to create an age variable and then reassign the value by 1 | ||
|
|
||
| const age = 33; | ||
| let age = 33; | ||
| age = age + 1; | ||
| console.log(age); | ||
|
|
||
| // The code above will output 34, because we are reassigning the value of age | ||
| // to be the current value of age plus 1. So, 33 + 1 = 34. | ||
| // The error in the code is that it was trying to reassign a value to a variable | ||
| // that was declared with let. This is not allowed in JavaScript. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,8 @@ | ||
| // 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}`); | ||
|
|
||
| // The error happens because cityOfBirth is used before it is defined. JavaScript runs | ||
| // code from top to bottom, so the variable must be declared first. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,21 @@ | ||
| const cardNumber = 4533787178994213; | ||
| const last4Digits = cardNumber.slice(-4); | ||
| // const cardNumber = 4533787178994213; | ||
| // const last4Digits = cardNumber.slice(-4); | ||
|
|
||
| // The last4Digits variable should store the last 4 digits of cardNumber | ||
| // However, the code isn't working | ||
| // Before running the code, make and explain a prediction about why the code won't work | ||
| // Then run the code and see what error it gives. | ||
| // Consider: Why does it give this error? Is this what I predicted? If not, what's different? | ||
| // Then try updating the expression last4Digits is assigned to, in order to get the correct value | ||
|
|
||
| // Prediction: The code will not work because slice() is being used on cardNumber, which is a number. | ||
| // The slice() method only works on strings or arrays, not numbers. | ||
| // So I predict JavaScript will throw an error saying that slice is not a function for a number. | ||
|
|
||
| const cardNumber = 4533787178994213; | ||
| const last4Digits = cardNumber.toString().slice(-4); | ||
| console.log(last4Digits); | ||
|
|
||
| // After running the code, I got the output "4213", which is the last 4 digits of the card number. | ||
| // This is what i expected. | ||
| //testing |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,12 @@ | ||
| const 12HourClockTime = "20:53"; | ||
| const 24hourClockTime = "08:53"; | ||
| // const 12HourClockTime = "20:53"; | ||
| // const 24hourClockTime = "08:53"; | ||
|
|
||
| // Variables cannot start with a number. | ||
| // When JavaScript tries to run the code, it throws a SyntaxError. | ||
| // To fix the error we can rename the variable to start with a letter, a | ||
| // underescore_ or a dollar sign $ instead of a number. | ||
|
|
||
| const twelveHourClockTime = "8:53"; | ||
| const twentyFourHourClockTime = "08:53"; | ||
|
|
||
| // I changed the time to match the 12 and 24 hr clock format. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1,78 @@ | ||
| const movieLength = 8784; // length of movie in seconds | ||
| //const movieLength = 8784; // length of movie in seconds | ||
|
|
||
| const remainingSeconds = movieLength % 60; | ||
| const totalMinutes = (movieLength - remainingSeconds) / 60; | ||
| //const remainingSeconds = movieLength % 60; | ||
| //const totalMinutes = (movieLength - remainingSeconds) / 60; | ||
|
|
||
| const remainingMinutes = totalMinutes % 60; | ||
| const totalHours = (totalMinutes - remainingMinutes) / 60; | ||
| //const remainingMinutes = totalMinutes % 60; | ||
| //const totalHours = (totalMinutes - remainingMinutes) / 60; | ||
|
|
||
| const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`; | ||
| console.log(result); | ||
| //const result = `${totalHours}:${remainingMinutes}:${remainingSeconds}`; | ||
| //console.log(result); | ||
|
|
||
| // For the piece of code above, read the code and then answer the following questions | ||
|
|
||
| // a) How many variable declarations are there in this program? | ||
|
|
||
| // There are 6 variable declarations in this program. They are on lines 1, 3, 5, 7, | ||
| // 9 and 11. The variables being declared are movieLength, remainingSeconds, totalMinutes, | ||
| // remainingMinutes, totalHours and result. | ||
|
|
||
|
|
||
|
|
||
| // b) How many function calls are there? | ||
|
|
||
| // There is 1 function calls in this program. It is on lines 10. The functions is the | ||
| // console.log(). | ||
|
|
||
|
|
||
|
|
||
| // c) Using documentation, explain what the expression movieLength % 60 represents | ||
| // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators | ||
|
|
||
| // % is the remainder operator. | ||
| // movieLength % 60 - This gives the remainder when movieLength is divided by 60. | ||
| // Since there are 60 seconds in a minute, this tells us how many leftover seconds there are | ||
| // after counting full minutes. | ||
| // For example, 8784 % 60 = 24. So there are 24 seconds left after counting the minutes. | ||
|
|
||
|
|
||
|
|
||
| // d) Interpret line 4, what does the expression assigned to totalMinutes mean? | ||
|
|
||
| // movieLength - remainingSecond - This gives the total number of seconds in the movie after | ||
| // removing the leftover seconds. | ||
| // Divide by 60 - this converst the seconds into total full minutes. eg. (8784 - 24) = 8760, 8760 / 60 = 146. | ||
|
|
||
|
|
||
|
|
||
| // e) What do you think the variable result represents? Can you think of a better name for this variable? | ||
|
|
||
| // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? Explain your answer | ||
| // The variable result is a string that represents the movie length in hours:minutes:seconds | ||
| // For example: 2:26:24 (2 hours, 26 minutes, 24 seconds) | ||
| // Better name can be movieDurationString. | ||
|
|
||
|
|
||
|
|
||
| // f) Try experimenting with different values of movieLength. Will this code work for all values of movieLength? | ||
| // Explain your answer | ||
|
|
||
| // This code will work for all positive integer values of movieLength. It will correctly calculate the hours, | ||
| // minutes and seconds for any length of movie. However, if movieLength is negative or not an integer, the | ||
| // code may not work as intended. For example, if movieLength is -100, the calculations will not make sense | ||
| // in the context of a movie length. If movieLength is a decimal, it may also cause issues with the calculations. | ||
|
|
||
| const movieLength = 65; | ||
|
|
||
| const remainingSeconds = movieLength % 60; | ||
| const totalMinutes = (movieLength - remainingSeconds) / 60; | ||
|
|
||
| const remainingMinutes = totalMinutes % 60; | ||
| const totalHours = (totalMinutes - remainingMinutes) / 60; | ||
|
|
||
| const formattedHours = String(totalHours).padStart(2, "0"); | ||
| const formattedMinutes = String(remainingMinutes).padStart(2, "0"); | ||
| const formattedSeconds = String(remainingSeconds).padStart(2, "0"); | ||
|
|
||
| const movieDurationString = `${formattedHours}:${formattedMinutes}:${formattedSeconds}`; | ||
|
|
||
| console.log(movieDurationString); // 00:01:05 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,38 @@ | ||
| // 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 "" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,32 @@ | ||
| // 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%" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 thinking to consider handling the empty string error.
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.
Thank you :)