Skip to content

ZA | MAY-2025 | Shannon Davids | Sprint-2 | Module-Structuring-and-Testing-Data #615

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 11 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
10 changes: 8 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,17 @@

// 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;
}
} */

// =============> write your explanation here
//syntax error str we are trying to use str to declare a variable why at the same time using it as the parameter for our function
// =============> write your new code here
function capitalise(str) {
let capString = `${str[0].toUpperCase()}${str.slice(1)}`;
return capString;
}
console.log(capitalise("shannon"));
13 changes: 11 additions & 2 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
// Predict and explain first...
// using function parameter to declare a variable will throw up a syntax error

// Why will an error occur when this program runs?
// =============> write your prediction here
//syntax error using function parameter to declare variable

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

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

return percentage;
}

console.log(decimalNumber);
console.log(decimalNumber);*/

// =============> write your explanation here
// its a function to convert a decimal number into a percentage

// 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.1));
18 changes: 12 additions & 6 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@

// Predict and explain first BEFORE you run any code...
//function should square number but i feel like will thro up an error cause the parameter name is invalid

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

// =============> write your prediction of the error here

function square(3) {
return num * num;
// error cause the parameter cant be a number that not valid variable name
/*
function square(num) {
return num * num;
}
console.log(square(3));*/

// =============> write the error message here
//Syntax error Unexpected number

// =============> explain this error message here
// there is a number 3 being use as a parameter and that is not valid variable naming

// Finally, correct the code to fix the problem

// =============> write your new code here


function square(num) {
return num * num;
}
console.log(square(3));
11 changes: 9 additions & 2 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 function will run but return nothing cause theres nothing being returned in this function
/*
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
//theres nothing being returned in this function

// 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)}`);
12 changes: 10 additions & 2 deletions Sprint-2/2-mandatory-debug/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
// Predict and explain first...
// =============> write your prediction here

//sum will be undefined
/*
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
// there is a semicolon after the return statement which automatically end the return statement
//the line after it is then completely ignored
// 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)}`);
19 changes: 17 additions & 2 deletions 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

/*
const num = 103;

function getLastDigit() {
Expand All @@ -12,13 +12,28 @@ function getLastDigit() {
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
// the return statement is using the num which is declared as a variable with a value before the function
// because getLastDigit does not have a parameters assigned to it.
// 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
// num had already been declared as variable with the value of 103 and not set as the parameter for the function
// so when getLasDigit is called the it draws directly from the variable num
6 changes: 4 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,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
}
//return the BMI of someone based off their weight and height
return (weight / (height * height)).toFixed(1);
}
console.log(calculateBMI(208, 1.8));
4 changes: 4 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,7 @@
// 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(string) {
return string.toUpperCase().replaceAll(" ", "_");
}
console.log(upperSnakeCase("Hello my name is shannon davids"));
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");

return `£${pounds}.${pence}`;
}
console.log(toPounds("399p"));
console.log(toPounds("15p"));
console.log(toPounds("2950p"));
console.log(toPounds("12457p"));
console.log(toPounds("1641632p"));
8 changes: 8 additions & 0 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ function formatTimeDisplay(seconds) {

return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
}
console.log(formatTimeDisplay(61));

// You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit
// to help you answer these questions
Expand All @@ -18,17 +19,24 @@ function formatTimeDisplay(seconds) {

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

// 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 because the last pad call is for remaining seconds
// so that would be 61 % 60 = 1 which leads to pad(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 because 1 becomes the string "1" and padStart(2, "0")ensures that its atleast 2 characters long
//by adding a 0 from start