-
-
Notifications
You must be signed in to change notification settings - Fork 219
Sheffield | 25-ITP-SEP | Jak Rhodes-Smith | Sprint 2 | coursework/sprint-2 #715
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
47f18bb
f682c6c
29f55b4
4361e74
f7c0a35
23a25ca
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,18 @@ | ||
// Predict and explain first... | ||
// =============> write your prediction here | ||
// The function is supposed to convert the first character of a string to uppercase | ||
// There will be a syntax error due to the redeclaration of the variable 'str' | ||
|
||
// call the function capitalise with a string input | ||
// interpret the error message and figure out why an error is occurring | ||
capitalise("hello"); | ||
|
||
// function capitalise(str) { | ||
// let str = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
// return str; | ||
// } | ||
|
||
// The error occurred because 'str' was declared again with 'let', which is not allowed. | ||
|
||
function capitalise(str) { | ||
let str = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
return str; | ||
return `${str[0].toUpperCase()}${str.slice(1)}`; | ||
} | ||
|
||
// =============> write your explanation here | ||
// =============> write your new code here |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,26 @@ | ||
// Predict and explain first... | ||
// The function is intended to take a decimal number and convert it to a percentage (e.g. 1.5 => 150%) | ||
|
||
// Why will an error occur when this program runs? | ||
// =============> write your prediction here | ||
// There will be a syntax error due to the redeclaration of the variable 'decimalNumber' | ||
|
||
// Try playing computer with the example to work out what is going on | ||
|
||
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 | ||
// Redeclaring the variable decimalNumber causes a syntax error | ||
// The parameter decimalNumber cannot be logged to the console as it is a parameter and holds no value until the function is called | ||
|
||
// Finally, correct the code to fix the problem | ||
// =============> write your new code here | ||
function convertToPercentage(decimalNumber) { | ||
return `${decimalNumber * 100}%`; | ||
} | ||
|
||
console.log(convertToPercentage(0.5)); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,28 @@ | ||
|
||
// 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 | ||
// A number is not a valid parameter (Syntax Error) | ||
|
||
function square(3) { | ||
return num * num; | ||
} | ||
// function square(3) { | ||
// return num * num; | ||
// } | ||
|
||
// =============> write the error message here | ||
// Failed to instrument Sprint-2/1-key-errors/2.js | ||
// 7 | | ||
// > 8 | function square(3) { | ||
// | ^ SyntaxError: Unexpected token (8:16) | ||
// 9 | return num * num; | ||
// 10 | } | ||
// 11 | | ||
// at <rootDir>/Sprint-2/1-key-errors/2.js:8:16 | ||
|
||
// =============> explain this error message here | ||
// parameter names cannot start with a digit | ||
|
||
// Finally, correct the code to fix the problem | ||
|
||
// =============> write your new code here | ||
|
||
function square(num) { | ||
return num * num; | ||
} | ||
|
||
console.log(square(3)); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,19 @@ | ||
// Predict and explain first... | ||
|
||
// =============> write your prediction here | ||
// The result of a * b will be printed to the console | ||
// Since the function does not return any value, it will return 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 | ||
// Because the function is evaluated before the string is printed to the console, it will log the result of a * b to the console first, but because it does not return a value, the next console.log of the string will print 'The result of multiplying 10 and 32 is undefined' | ||
|
||
// 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 | ||
// The function is intended to take 2 arguments and return the sum | ||
// it will return undefined | ||
|
||
// function sum(a, b) { | ||
// return; | ||
// a + b; | ||
// } | ||
|
||
// console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); | ||
|
||
// it currently has a return statement that ends the function prematurely, causing it to return undefined | ||
// Finally, correct the code to fix the problem | ||
function sum(a, b) { | ||
return; | ||
a + b; | ||
return a + b; | ||
} | ||
|
||
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,35 @@ | ||
// Predict and explain first... | ||
// The function intends to take a number and return it's last digit | ||
// Each call of the function will return the same result | ||
|
||
// Predict the output of the following code: | ||
// =============> Write your prediction here | ||
// 'The last digit of 42 is 3' | ||
// 'The last digit of 105 is 3' | ||
// 'The last digit of 806 is 3' | ||
|
||
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 was not correctly defined to accept an argument, It was using a fixed variable instead | ||
|
||
// Finally, correct the code to fix the problem | ||
// =============> write your new code here | ||
function getLastDigit(num) { | ||
return num.toString().slice(-1); | ||
} | ||
|
||
// This program should tell the user the last digit of each number. | ||
// Explain why getLastDigit is not working properly - correct the problem | ||
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)}`); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,8 +4,9 @@ | |
|
||
function formatAs12HourClock(time) { | ||
const hours = Number(time.slice(0, 2)); | ||
if (hours > 12) { | ||
return `${hours - 12}:00 pm`; | ||
const minutes = time.slice(3, 5); | ||
if (hours >= 12) { | ||
return `${hours !== 12 ? hours - 12 : hours}:${minutes} pm`; | ||
} | ||
return `${time} am`; | ||
} | ||
|
@@ -23,3 +24,75 @@ console.assert( | |
currentOutput2 === targetOutput2, | ||
`current output: ${currentOutput2}, target output: ${targetOutput2}` | ||
); | ||
|
||
// Midnight | ||
const outMidnight = formatAs12HourClock("00:00"); | ||
const targetMidnight = "00:00 am"; | ||
Comment on lines
+29
to
+30
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.
|
||
console.assert( | ||
outMidnight === targetMidnight, | ||
`current output: ${outMidnight}, target output: ${targetMidnight}` | ||
); | ||
|
||
// Noon | ||
const outNoon = formatAs12HourClock("12:00"); | ||
const targetNoon = "12:00 pm"; | ||
console.assert( | ||
outNoon === targetNoon, | ||
`current output: ${outNoon}, target output: ${targetNoon}` | ||
); | ||
|
||
// 1 PM | ||
const out13 = formatAs12HourClock("13:00"); | ||
const target13 = "1:00 pm"; | ||
console.assert( | ||
out13 === target13, | ||
`current output: ${out13}, target output: ${target13}` | ||
); | ||
|
||
// 12:59 PM | ||
const out1259 = formatAs12HourClock("12:59"); | ||
const target1259 = "12:59 pm"; | ||
console.assert( | ||
out1259 === target1259, | ||
`current output: ${out1259}, target output: ${target1259}` | ||
); | ||
|
||
// 11:59 PM | ||
const out2359 = formatAs12HourClock("23:59"); | ||
const target2359 = "11:59 pm"; | ||
console.assert( | ||
out2359 === target2359, | ||
`current output: ${out2359}, target output: ${target2359}` | ||
); | ||
|
||
// 01:00 AM | ||
const out1 = formatAs12HourClock("01:00"); | ||
const target1 = "01:00 am"; | ||
console.assert( | ||
out1 === target1, | ||
`current output: ${out1}, target output: ${target1}` | ||
); | ||
Comment on lines
+69
to
+74
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. Some return values are padded with zeroes and some aren't. |
||
|
||
// 10:30 AM | ||
const out1030 = formatAs12HourClock("10:30"); | ||
const target1030 = "10:30 am"; | ||
console.assert( | ||
out1030 === target1030, | ||
`current output: ${out1030}, target output: ${target1030}` | ||
); | ||
|
||
// 15:45 PM | ||
const out1545 = formatAs12HourClock("15:45"); | ||
const target1545 = "3:45 pm"; | ||
console.assert( | ||
out1545 === target1545, | ||
`current output: ${out1545}, target output: ${target1545}` | ||
); | ||
|
||
// Edge: single digit hour | ||
const outSingle = formatAs12HourClock("7:45"); | ||
const targetSingle = "7:45 am"; | ||
console.assert( | ||
outSingle === targetSingle, | ||
`current output: ${outSingle}, target output: ${targetSingle}` | ||
); |
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.
What type of value do you expect the function to return? A number or a string?
Does your function return the type of value you expect?