Skip to content
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

CYF-ITP-South Africa | Rashaad Ebrahim | Module-Structuring-and-Testing-Data | Week 2 #219

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion Sprint-2/debug/0.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Predict and explain first...

function multiply(a, b) {
console.log(a * b);
return (a * b);
}

console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
3 changes: 1 addition & 2 deletions Sprint-2/debug/1.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// Predict and explain first...

function sum(a, b) {
return;
a + b;
return a + b;
}

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

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

function getLastDigit() {
function getLastDigit(num) {
return num.toString().slice(-1);
}

Expand All @@ -12,3 +12,6 @@ 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 is declared as a constant variable on ln 3 and all results from then on are based on this value.
// I have changed the function by having num as an argument in the function.
Rashaad-Ebrahim marked this conversation as resolved.
Show resolved Hide resolved
7 changes: 5 additions & 2 deletions Sprint-2/errors/0.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring
// What woll happened is that str is indicated as the argument for the function and should not be declared within the function.

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

console.log(capitalise("who let the dogs out?"));
8 changes: 5 additions & 3 deletions Sprint-2/errors/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
// Predict and explain first...

// Why will an error occur when this program runs?
// There will be an error becuase the arguement is being declared as a constant variable inside of the functions. The console.log() does not call the function, it is trying to log the arguement which it doesn't have access to because of scope..

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

function convertToPercentage(decimalNumber) {
const decimalNumber = 0.5;
function convertToPercentage(decimalNumber) { // decimalNumber is shown as an argument
// const decimalNumber = 0.5; // decimalNumber is then declared as a constant variable
const percentage = `${decimalNumber * 100}%`;

return percentage;
}

console.log(decimalNumber);
console.log(decimalNumber); // unable to log variable becuase of scope.
3 changes: 2 additions & 1 deletion Sprint-2/errors/2.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@

// Predict and explain first...
// There will be an error as a number is indicated as an argument. I would think that an arguement should also follow the rule of JavaScript variable naming conventions of not starting with a number.

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

function square(3) {
function square(num) { // Function argument should be num as in function(num)
return num * num;
}

Expand Down
79 changes: 67 additions & 12 deletions Sprint-2/extend/format-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,77 @@

function formatAs12HourClock(time) {
const hours = Number(time.slice(0, 2));
const minutes = time.slice(time.indexOf(":") + 1, time.indexOf(":") + 3);
// console.log(minutes);
if (hours === 0) {
return `${hours + 12}:${minutes} am`;
Rashaad-Ebrahim marked this conversation as resolved.
Show resolved Hide resolved
}
if (hours > 12) {
return `${hours - 12}:00 pm`;
return `${hours - 12}:${minutes} pm`;
}
return `${time} am`;
}

const currentOutput = formatAs12HourClock("08:00");
const targetOutput = "08:00 am";
console.assert(
currentOutput === targetOutput,
`current output: ${currentOutput}, target output: ${targetOutput}`
);

const currentOutput2 = formatAs12HourClock("23:00");
const targetOutput2 = "11:00 pm";
console.assert(
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2}`
// Chose to create an array of test objects
const testData = [
{
currentOutput: formatAs12HourClock("08:00"),
targetOutput: "08:00 am",
},
{
currentOutput: formatAs12HourClock("23:00"),
targetOutput: "11:00 pm",
},
{
currentOutput: formatAs12HourClock("00:00"),
targetOutput: "12:00 am",
},
{
currentOutput: formatAs12HourClock("08:15"),
targetOutput: "08:15 am",
},
{
currentOutput: formatAs12HourClock("21:25"),
targetOutput: "9:25 pm",
},
{
currentOutput: formatAs12HourClock("23:59"),
targetOutput: "11:59 pm",
},
{
currentOutput: formatAs12HourClock("11:59"),
targetOutput: "11:59 am",
},
{
currentOutput: formatAs12HourClock("4:05"),
targetOutput: "4:05 am",
},
{
currentOutput: formatAs12HourClock("13:05"),
targetOutput: "1:05 pm",
},
];

Rashaad-Ebrahim marked this conversation as resolved.
Show resolved Hide resolved
// Used the map array method to iterate through the test data.
testData.map((data) =>
console.assert(
data.currentOutput === data.targetOutput,
`current output: ${data.currentOutput}, target output: ${data.targetOutput}`
)
);

// const currentOutput = formatAs12HourClock("08:00");
// const targetOutput = "08:00 am";
// console.assert(
// currentOutput === targetOutput,
// `current output: ${currentOutput}, target output: ${targetOutput}`
// );

// const currentOutput2 = formatAs12HourClock("23:00");
// const targetOutput2 = "11:00 pm";
// console.assert(
// currentOutput2 === targetOutput2,
// `current output: ${currentOutput2}, target output: ${targetOutput2}`
// );

5 changes: 5 additions & 0 deletions Sprint-2/implement/bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@
// Given someone's weight in kg and height in metres
// Then when we call this function with the weight and height
// It should return their Body Mass Index to 1 decimal place

function calculateBMI(weight, height) {
const bmi = weight / height ** 2; // Calculates BMI
return Math.round(bmi * 10) / 10; // Implement the rounding to 1 decimal point
}
6 changes: 6 additions & 0 deletions Sprint-2/implement/cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,9 @@

// You will need to come up with an appropriate name for the function
// Use the string documentation to help you find a solution

function strToUpperSnakeCase(str) {
const upperCaseStr = str.toUpperCase();
return upperCaseStr.replaceAll(" ", "_");
// return upperCaseStr.split(" ").join('_'); // This would also work
}
32 changes: 32 additions & 0 deletions Sprint-2/implement/to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,35 @@
// 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(penceStr) {
const penceStringWithoutTrailingP = penceStr.substring(
0,
penceStr.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("1p"));
console.log(toPounds("85p"));
console.log(toPounds("599p"));
console.log(toPounds("3000p"));
console.log(toPounds("987654p"));

const currentOutput = toPounds("1p");
const targetOutput = "£0.01";
console.assert(
currentOutput === targetOutput,
`current output: ${currentOutput}, target output: ${targetOutput}`
);
5 changes: 5 additions & 0 deletions Sprint-2/implement/vat.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@
// Given a number,
// When I call this function with a number
// it returns the new price with VAT added on

function priceIncludingVAT(price) {
return Math.round(price * 1.2 * 100) / 100; // Added the rounding to cover edge cases
}

12 changes: 12 additions & 0 deletions Sprint-2/interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,31 @@ function formatTimeDisplay(seconds) {
)}`;
}

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

// Questions

// a) When formatTimeDisplay is called how many times will pad be called?

// pad will be 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?

// 0

// c) What is the return value of pad is called for the first time?

// '00'

// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer

// 1, when pad is called for the last time, the variable remainingSeconds is the argument. The value of remainingSeconds is derived from 'seconds % 60'. As seconds is 61, there is 1 remaining.

// e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer

// There is no return value assigned to num. There is however a return value assigned to pad, which is '01'