-
-
Notifications
You must be signed in to change notification settings - Fork 239
NW | 25-ITP-Sep | Ahmad Hmedan | Sprint 2 | Sprint 2 Coursework #783
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 5 commits
dd42ff0
4dffed1
b0b0990
ead2f59
785e8e7
10a99cc
daff116
95b4184
4488e38
e563c47
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 | ||
| // =============> I predict it will throw a syntaxerror because the parameter 'str' is already declare in the definition, | ||
| //and line 9 try to declare another variable with the same name so the error will be identifier 'str'has already been declared . | ||
|
|
||
|
|
||
| // 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; | ||
| } | ||
| // function capitalise(str) { | ||
| // let str = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| // return str; | ||
| // } | ||
| // capitalise("ahmadhmedan"); | ||
|
|
||
| // =============> write your explanation here | ||
| // =============> write your new code here | ||
| // =============> As I predict it will throw a SyntaxError: Identifier 'str' has already been declared | ||
| //because in JavaScript we can not redeclare the same variable in the same scope. | ||
| function Capitalise(str){ | ||
| return `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| } | ||
| console.log(Capitalise("ahmadHmedan")); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,19 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // =============> write your prediction here | ||
| // =============> it will throw a syntaxerror because I can not use nested console.log. | ||
|
|
||
| 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 | ||
| // =============> The result of multiplying 10 and 32 is undefined undefined because If I want to keep the value with me after the function finishes, I must use return. | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
| function multiply(a, b) { | ||
| return(a*b); | ||
| } | ||
|
|
||
| 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 | ||
| // =============> It will not return the value. | ||
|
|
||
| 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 | ||
| // =============> any think we do after return will not coding. | ||
| // 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,39 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // Predict the output of the following code: | ||
| // =============> Write your prediction here | ||
| // =============> first it will convert the number to string then it will slice the last character that function | ||
| //then will print | ||
| //The last digit of 42 os 2 | ||
| //The last digit of 105 os 5 | ||
| //The last digit of 806 os 6 | ||
|
|
||
| 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 | ||
| // =============> 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 | ||
| // =============> Because the function always takes the global variable witch is 103 | ||
| //the output will be the same witch 3. | ||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
| // =============> write your new code her | ||
|
|
||
|
|
||
| function getLastDigit(num) { | ||
| 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)}`); | ||
| // 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 |
|---|---|---|
|
|
@@ -15,5 +15,7 @@ | |
| // 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 | ||
| } | ||
| let BMI=weight/(height*height); | ||
|
||
| return BMI.toFixed(1); | ||
| } | ||
| console.log(calculateBMI(70,1.73)); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,3 +14,22 @@ | |
| // 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 ConvertToUpperSnakeCase(s1) | ||
| { | ||
| let result = ""; | ||
| for(let i=0;i<s1.length;i++) | ||
|
||
| { | ||
|
|
||
| if(s1[i]===" ") | ||
| { | ||
| result+=s1[i]="_"; | ||
| } | ||
| else | ||
| result+=s1[i]; | ||
| } | ||
| let UPPER_SNAKE_CASE=result.toUpperCase(); | ||
|
||
| return UPPER_SNAKE_CASE; | ||
| } | ||
| console.log(ConvertToUpperSnakeCase("Ahmad Hmedan")); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,3 +4,14 @@ | |
| // 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 toPound(penceString) | ||
|
||
| { | ||
| const penceWithoutP=penceString.substring(0,penceString.length-1); | ||
| const paddedPence=penceWithoutP.padStart(3,"0"); | ||
| const pound=paddedPence.substring(0,paddedPence.length-2); | ||
| const Pence=paddedPence.substring(paddedPence.length-2); | ||
| return `£${pound}.${Pence}` | ||
| } | ||
| console.log(toPound("100p")); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,3 +23,68 @@ console.assert( | |
| currentOutput2 === targetOutput2, | ||
| `current output: ${currentOutput2}, target output: ${targetOutput2}` | ||
| ); | ||
| const currentOutput3=formatAs12HourClock("00:00"); | ||
| const targetOutput3="12:00 am"; | ||
| console.assert(currentOutput3===targetOutput3, | ||
| `current output: ${currentOutput3}, target output: ${targetOutput3}` | ||
| ) | ||
| const currentOutput4=formatAs12HourClock("12:00"); | ||
| const targetOutput4="12:00 pm"; | ||
| console.assert(currentOutput4===targetOutput4, | ||
| `current output: ${currentOutput4}, target output: ${targetOutput4}` | ||
| ) | ||
|
|
||
|
|
||
| // This is my version | ||
| function TimeAs12hours(time) { | ||
| const hours = Number(time.slice(0, 2)); | ||
| const minutes = time.slice(3); | ||
| if (hours === 0) { | ||
| return `12:${minutes} am`; | ||
| } else if (hours > 12) { | ||
| return `${hours - 12}:${minutes} pm`; | ||
| } else if (hours === 12) { | ||
| return `${time} pm`; | ||
| } | ||
| return `${time} am`; | ||
| } | ||
|
|
||
| let currentTime = TimeAs12hours("12:02"); | ||
| let targetTime = "12:02 pm"; | ||
| console.assert( | ||
| currentTime === targetTime, | ||
| `current time:${currentTime}, Target Time:${targetTime}` | ||
| ); | ||
| currentTime = TimeAs12hours("10:30"); | ||
| targetTime = "10:30 am"; | ||
| console.assert( | ||
| currentTime === targetTime, | ||
| `current time:${currentTime}, Target Time:${targetTime}` | ||
| ); | ||
|
|
||
| currentTime = TimeAs12hours("04:30"); | ||
| targetTime = "04:30 am"; | ||
| console.assert( | ||
| currentTime === targetTime, | ||
| `current time:${currentTime}, Target Time:${targetTime}` | ||
| ); | ||
|
|
||
| currentTime = TimeAs12hours("13:30"); | ||
| targetTime = "1:30 pm"; | ||
| console.assert( | ||
| currentTime === targetTime, | ||
| `current time:${currentTime}, Target Time:${targetTime}` | ||
| ); | ||
| currentTime = TimeAs12hours("23:09"); | ||
| targetTime = "11:09 pm"; | ||
| console.assert( | ||
| currentTime === targetTime, | ||
| `current time:${currentTime}, Target Time:${targetTime}` | ||
| ); | ||
|
|
||
| currentTime = TimeAs12hours("00:00"); | ||
| targetTime = "12:00 am"; | ||
| console.assert( | ||
| currentTime === targetTime, | ||
| `current time:${currentTime}, Target Time:${targetTime}` | ||
| ); | ||
|
||
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.
In Javascript, we follow the convention of
camelCasefor functions and variables