Skip to content

ZA-CapeTown | TP-May-2025 | Dawud Vermeulen | Module-Structuring and Testing Data Sprint-2 #673

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 5 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
11 changes: 9 additions & 2 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@

// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring

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

// =============> write your explanation here
// the function takes string arg it gets the first character of the string and capitalises it. the .slice method gets the rest of the string and concats the capitalized first char. all this is assigned to var called str and returns the str.
// the error says the var str has already been used in the function, this is because the var str is declared in the function and then again in the return statement. we could fix it by removing the var keyword in the return statement.
// =============> write your new code here

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You've identified the problem - can you write some fixed code?


// function capitalize(str) {
// return `${str[0].toUpperCase()}${str.slice(1)}`;
// }

console.log(capitalise('hello earth'));
27 changes: 21 additions & 6 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,34 @@

// Why will an error occur when this program runs?
// =============> write your prediction here

// i think it gets a error cause the var decimalNumber is declared inside the function and used as the arg of the function. not sure of the error but it should throw a error
// 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
//function declared with arg. takes decimal number
// this is the error, the var decimalNumber is declared inside the function and then used as the arg of the function. this is not allowed in js.
// the decimal number is converted to percentage by multiplying it by 100 and concating a % sign
// returns the percentage
// prints to console the decimal number

// Finally, correct the code to fix the problem
// =============> write your new code here
/*
const decimalNumber = 0.5;

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

return percentage;
}

console.log(convertToPercentage(decimalNumber));
11 changes: 10 additions & 1 deletion Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,26 @@
// this function should square any number but instead we're going to get an error

// =============> write your prediction of the error here
// I predict that the error will be that the parameter 'num' is not defined because it is not declared before being used in the function.

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

// =============> write the error message here

// SyntaxError: Unexpected number
// =============> explain this error message here
// This error message occurs because the function parameter is not defined correctly. In JavaScript, function parameters must be valid identifiers, and using a number directly (like 3) is not allowed.

// Finally, correct the code to fix the problem

// =============> write your new code here
/*
let num = 3;

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


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

// function is expected to multiply two numbers and log the result. it starts by taking in args 'a' and 'b', then it logs the product of 'a' and 'b'.
// then it calls the function with 10 and 32 and tries to log the result of the function call.
// However, the function does not return a value, it only logs the product. Therefore,
// =============> write your prediction here

// undefined
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

// no return in the function so its always 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)}`);

*/
9 changes: 9 additions & 0 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Predict and explain first...
// =============> write your prediction here
// it wants to add to args 'a' and 'b' and log the result. in the function body it makes a return and closes then on a new line does the addition of 'a' and 'b'.
// get a error like undefined maybe.

function sum(a, b) {
return;
Expand All @@ -9,5 +11,12 @@ function sum(a, b) {
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

// =============> write your explanation here
// the function does not return the sum of 'a' and 'b', it just returns undefined because the return statement gets closed and is not followed by any value.
// 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)}`);
*/
16 changes: 15 additions & 1 deletion Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// Predict the output of the following code:
// =============> Write your prediction here

// output: last degit of 42 is undefined. the function is not taking in any arguments. num is has hardcoded. the function currently returns the last digit of num which is 103.
const num = 103;

function getLastDigit() {
Expand All @@ -15,10 +15,24 @@ 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
/* $ node 2.js
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
// output is always 3 cause num is hardcoded to 103. the function does not take in any args, so it always returns the last digit of 103
// Finally, correct the code to fix the problem
// =============> write your new code here
/*
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
29 changes: 28 additions & 1 deletion Sprint-2/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Below are the steps for how BMI is calculated

const { createTestScheduler } = require("jest");

// The BMI calculation divides an adult's weight in kilograms (kg) by their height in metres (m) squared.

// For example, if you weigh 70kg (around 11 stone) and are 1.73m (around 5 feet 8 inches) tall, you work out your BMI by:
Expand All @@ -16,4 +18,29 @@

function calculateBMI(weight, height) {
// return the BMI of someone based off their weight and height
}
const bmi = weight / (height * height);
const value= bmi.toFixed(1);

let category;
if (value < 18.5) category = "Underweight";
else if (value < 25) category = "Healthy";
else if (value < 30) category = "Overweight";
else catergory = "Please get in touch with your doctor";

return { value, category };
}

//testing testing 1 2
// console.log(`My BMI is ${calculateBMI(65.8, 1.69)}`); //DV
// console.log(`My BMI is ${calculateBMI(58.0, 1.73)}`); //TMKC
// console.log(`My BMI is ${calculateBMI(22.5, 1.22)}`); // SV
const tests = [
{ w: 65.8, h: 1.69 }, //DV
{ w: 58.0, h: 1.73 }, // TMKC
{ w: 22.5, h: 1.22 } // SV according to the calc our kids underweight but the chubbiest of us all
];

tests.forEach(({w, h}) => {
const { value, category } = calculateBMI(w, h);
console.log( `BMI ${value}, category: ${category}`);
});
10 changes: 10 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,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
function toUpperSnakeCase(input) {
// split the words by spaces
const words = input.split(' ');
// map each of the words to uppercase and join them with underscores
const upperSnakeCase = words.map(word => word.toUpperCase()).join('_');
return upperSnakeCase;
}

//test
console.log(toUpperSnakeCase("why do people run")); // "WHY_DO_PEOPLE_RUN"
25 changes: 25 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,28 @@
// 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) {
// This program takes a string representing a price in pence

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");

return`£${pounds}.${pence}`;
}
console.log(toPounds("123p")); // "£1.23"
console.log(toPounds("4567p")); // "£45.67"
console.log(toPounds("89p")); // "£0.89"
console.log(toPounds("5p")); // "£0.05"
10 changes: 6 additions & 4 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,19 @@ function formatTimeDisplay(seconds) {

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

// 3
// 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

// 0?
// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// "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

// 1
// 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
// "01"