Skip to content

ZA | ITP May 25 | OBERT TILIMANDZI| MODULE-STRUCTURING AND TESTING DATA | SPRINT 2 #613

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 7 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
1 change: 1 addition & 0 deletions Sprint-1/1-key-exercises/1-count.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ count = count + 1;

// Line 1 is a variable declaration, creating the count variable with an initial value of 0
// Describe what line 3 is doing, in particular focus on what = is doing
// Answer: "count + 1" is a value being assigned to the variable count and "=" is the assignement operator used to assign values to a variable
1 change: 1 addition & 0 deletions Sprint-1/Module-Structuring-and-Testing-Data
Submodule Module-Structuring-and-Testing-Data added at 9b108f
11 changes: 6 additions & 5 deletions Sprint-2/1-key-errors/0.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// Predict and explain first...
// =============> write your prediction here
// =============> function looks good, this will capitalize first letter of the string

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

// "str" variable was declared twice in the code we shall have to rename it

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

// =============> write your explanation here
// =============> write your new code here
console.log(capitalise("peter"));
24 changes: 15 additions & 9 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
// Predict and explain first...

// Why will an error occur when this program runs?
// =============> write your prediction here
// =============> this wont work as the variable was assigned to the argument name

// 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
// =============>'decimalNumber' was already declared so we need to remove it and get add the argument in the console rather

// 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.7))
16 changes: 10 additions & 6 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
// =============> we will get an error message

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

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

// =============> explain this error message here
// =============> a number was used to name a parameter and also cannot start the name with a number moreover the retun was named different

// Finally, correct the code to fix the problem

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


function square(num) {
return num * num;
}
console.log(square(3))
19 changes: 12 additions & 7 deletions Sprint-2/2-mandatory-debug/0.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
// Predict and explain first...

// =============> write your prediction here
// =============> code will give us 2 results

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
// =============> we have duplicate console and missing return, so our function will only print and not execute

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

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
// =============> the return statement is void and not connected with the expression
// 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)}`);
26 changes: 17 additions & 9 deletions Sprint-2/2-mandatory-debug/2.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
// Predict and explain first...

// Predict the output of the following code:
// =============> Write your prediction here
// =============> We will get an error since num is declared as a global variable

const num = 103;
// const num = 103;

function getLastDigit() {
// 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)}`);

// Now run the code and compare the output to your prediction
// =============> the code worked
// the function is only working using one variable
// =============> this function is not taking any arguments
// Finally, correct the code to fix the problem

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)}`);

// Now run the code and compare the output to your prediction
// =============> write the output here
// Explain why the output is the way it is
// =============> write your explanation here
// Finally, correct the code to fix the problem
// =============> write your new code here

// This program should tell the user the last digit of each number.
// Explain why getLastDigit is not working properly - correct the problem
11 changes: 10 additions & 1 deletion Sprint-2/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,13 @@

function calculateBMI(weight, height) {
// return the BMI of someone based off their weight and height
}
// first square the height (height × height)
// divide the weight by that squared height value
// format the result to 1 decimal place

const heightInput = height * height;
const result = weight / heightInput;
return result.toFixed(1);

}
console.log(calculateBMI(101, 1.3))
5 changes: 5 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,8 @@
// 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 allCapsNoSpace(textInput) {
return textInput.toUpperCase().replace(/ /g, '_');
};
console.log(allCapsNoSpace("take the spaces away from this line"));
19 changes: 19 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,22 @@
// 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("18950p"));






21 changes: 12 additions & 9 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
function pad(num) {
return num.toString().padStart(2, "0");
}
// function pad(num) {
// return num.toString().padStart(2, "0");
// }

function formatTimeDisplay(seconds) {
const pad = num => num.toString().padStart(2, "0");
const remainingSeconds = seconds % 60;

const totalMinutes = (seconds - remainingSeconds) / 60;
const remainingMinutes = totalMinutes % 60;
const totalHours = (totalMinutes - remainingMinutes) / 60;

const totalHours = (totalMinutes - remainingMinutes) / 60;
return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
}
console.log(formatTimeDisplay("697541"))

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

// Questions

// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here
// =============> pad will be called three times, hours, minutes and seconds

// 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, there are no hours

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// =============> 00 hours

// 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
// =============> its 1 second

// 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
// =============> its 01 seconds
2 changes: 1 addition & 1 deletion Sprint-3/1-key-implement/3-get-card-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function assertEquals(actualOutput, targetOutput) {
// When the function getCardValue is called with this card string as input,
// Then it should return the numerical card value
const aceofSpades = getCardValue("A♠");
assertEquals(aceofSpades, 11);
assertEquals(aceofSpades, '11');

// Handle Number Cards (2-10):
// Given a card with a rank between "2" and "9",
Expand Down