Skip to content

ZA-ITP-May-2025 | Christian Mayamba | Module-Structuring and Testing Data | Week 2 #583

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
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Predict and explain first...
// =============> write your prediction here
// =============> The function capitalise is trying to take a string input,
// convert the first characater to uppercase and extract characters from
// the second to the end of the string.

// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring
Expand All @@ -9,5 +11,16 @@ function capitalise(str) {
return str;
}

// =============> write your explanation here
// =============> write your new code here
capitalise("hello");

// =============> The error "Identifier 'str' has already been declared" is occuring
// because the variable `str` is being declared twice in the function.

// =============> This is my new code:

function capitalise(str) {
let name = `${str[0].toUpperCase()}${str.slice(1)}`;
return name;
}

capitalise("hello");
18 changes: 15 additions & 3 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Predict and explain first...

// Why will an error occur when this program runs?
// =============> write your prediction here
// =============> My prediction is that the code will not give an error,
// but it will be only giving 50% as a result when the function is called,
// because the variable 'decimal number' is assigned the value of 0.5.

// Try playing computer with the example to work out what is going on

Expand All @@ -14,7 +16,17 @@ function convertToPercentage(decimalNumber) {

console.log(decimalNumber);

// =============> write your explanation here
// =============> The error "Identifier 'decimalNumber' has already been declared" is occurring
// because the variable `decimalNumber` is being declared twice in the function.
// And console.log cannot access the variable `decimalNumber` because of its scope.

// Finally, correct the code to fix the problem
// =============> write your new code here
// =============> Here's my code:

function convertToPercentage(decimalNumber) {
const percentage = `${decimalNumber * 100}%`;

return percentage;
}

console.log(convertToPercentage(0.7));
12 changes: 8 additions & 4 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@

// this function should square any number but instead we're going to get an error

// =============> write your prediction of the error here
// =============> The 'num' variable is not defined in the function scope,
// so when we try to use it, it will throw an error.

function square(3) {
return num * num;
}

// =============> write the error message here
// =============> The error is "SyntaxError: Unexpected number"

// =============> explain this error message here
// =============> The number '3" is used where it shouldn't be, as a parameter.

// Finally, correct the code to fix the problem

// =============> write your new code here
// =============> write your new code here:
function square(num) {
return num * num;
}


13 changes: 10 additions & 3 deletions Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
// Predict and explain first...

// =============> write your prediction here
// =============> The code won't run because the function `multiply`
// is not making any calculation and it is not returning any value.

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 result of multiplying 10 and 32 is undefined because
// 'multiply' has not been properly defined to return a value.

// Finally, correct the code to fix the problem
// =============> write your new code here
// =============> Here's the new code:
function multiply(a, b) {
return a * b;
}

console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
12 changes: 9 additions & 3 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Predict and explain first...
// =============> write your prediction here
// =============> The syntax of the function `sum` is incorrect.

function sum(a, b) {
return;
Expand All @@ -8,6 +8,12 @@ function sum(a, b) {

console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

// =============> write your explanation here
// =============> "a + b" should be on the same line as the return statement.

// Finally, correct the code to fix the problem
// =============> write your new code here
// =============> Here's the new code:
function sum(a, b) {
return a + b;
}

console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
23 changes: 17 additions & 6 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Predict and explain first...

// Predict the output of the following code:
// =============> Write your prediction here
// =============> Since 'num' is defined as a global variable,
// the function getLastDigit will always return the last digit of 103.

const num = 103;

Expand All @@ -14,11 +15,21 @@ 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
// =============> 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
// =============> The output is always 3 because the variable `num` is defined globally as 103,
// and the function `getLastDigit` is not using any parameter.

// Finally, correct the code to fix the problem
// =============> write your new code here
// =============> Here's the corrected code:
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)}`);
12 changes: 10 additions & 2 deletions Sprint-2/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,13 @@
// 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
}
const bmi = weight / (height * height);
return bmi.toFixed(1);
}

console.log(
`The BMI for a person weighing 100kg and 1.85m tall is ${calculateBMI(
70,
1.73
)}`
); // Expected output: 23.4
6 changes: 6 additions & 0 deletions Sprint-2/3-mandatory-implement/2-cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,9 @@
// 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 upperSnakeCase(str) {
let stringInput = `${str.replace(/ /g, "_")}`;
let stringInput_final = stringInput.toUpperCase();
return stringInput_final;
}
23 changes: 23 additions & 0 deletions Sprint-2/3-mandatory-implement/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,26 @@
// 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(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");

const finalResult = `£${pounds}.${pence}`;

return finalResult;
}

console.log(toPounds("399p"));
14 changes: 8 additions & 6 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,20 @@ function formatTimeDisplay(seconds) {
// Questions

// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here
// =============> 'pad' will be called 3 times when 'formatTimeDisplay' is called.

// Call formatTimeDisplay with an input of 61, now answer the following:

// b) What is the value assigned to num when pad is called for the first time?
// =============> write your answer here
// =============> The value assigned in this case will be 0.

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// c) What is the return value of pad when it's called for the first time?
// =============> The return value is '00'.

// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
// =============> write your answer here
// =============> the value assigned to 'num' is 1, because when pad is called for the last time,
// it is called with the 'remainingSeconds', which is 1 in this case.

// e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer
// =============> write your answer here
// =============> The return value assigned to 'num' is still 1 for the reasons cited above.
// The return value of pad might be different in this case, but it's not asked.
Loading