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

London | Zohreh-Kazemianpour | Structuring-and-testing-data | week3 | sprint 3 #223

Open
wants to merge 20 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
4 changes: 3 additions & 1 deletion Sprint-2/debug/0.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Predict and explain first...
//There is no return keyword so the function will not return any value

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

console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
//we got an error that the value is undefined to fix this we need to add return keyword
6 changes: 3 additions & 3 deletions Sprint-2/debug/1.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Predict and explain first...

// return and expression should be in the same line
function sum(a, b) {
return;
a + b;
return a + b;
}
// we got an error that the variable is undefine

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

const num = 103;

function getLastDigit() {

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

Expand Down
7 changes: 6 additions & 1 deletion Sprint-2/errors/0.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
// Predict and explain first...
//str is declared twice and for sure we will get an error

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

//As expected we got a syntaxError. We can correct the error by removing he let keyword

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

console.log(capitalise("hello"));
5 changes: 2 additions & 3 deletions Sprint-2/errors/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
// Predict and explain first...

//Amswer: the decimalNumber is hardcoded which means the function will not be reusable for different values. second problem is the scope that variable id defined. in this case we would not have access to decimalNumber outside of its function
// Why will an error occur when this program runs?
// Try playing computer with the example to work out what is going on

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

return percentage;
}

console.log(decimalNumber);
console.log(convertToPercentage(.5));
4 changes: 3 additions & 1 deletion Sprint-2/errors/2.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@

// Predict and explain first...
//Answer: in the function parameters num should be replace. with that we cn assign the variable num and then inside the function we will have access to it which we can modify it

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

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


console.log(square(3))
13 changes: 11 additions & 2 deletions Sprint-2/extend/format-time.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
// This is the latest solution to the problem from the prep.
// Your task is to write tests for as many different groups of input data or edge cases as you can, and fix any bugs you find.
// we have to consider minutes and two eage cases that comes to my mind : 12 and 00

function formatAs12HourClock(time) {
const hours = Number(time.slice(0, 2));
if (hours > 12) {
const minutes = time.slice(3,5)
if (hours === 0){
return `12:${minutes} am`
}else if (hours === 12){
return `12:${minutes} pm`
}else if(hours > 12) {
return `${hours - 12}:00 pm`;
}
}else
return `${time} am`;
}

Expand All @@ -22,3 +28,6 @@ console.assert(
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2}`
);

console.log(formatAs12HourClock("00:00"));
console.log(formatAs12HourClock("12:00"));
8 changes: 8 additions & 0 deletions Sprint-2/implement/bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,11 @@
// 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 calBMI (weight, height) {
let squareHeight = height * height;
return Number((weight / squareHeight).toFixed(1)) ;

}

console.log(calBMI(61, 1.61));
7 changes: 7 additions & 0 deletions Sprint-2/implement/cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,10 @@

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

function convertToUpperSnakeCase (str) {
return str.replaceAll(" ", "_").toUpperCase()

}

console.log(convertToUpperSnakeCase("lord of the rings"))
31 changes: 31 additions & 0 deletions Sprint-2/implement/to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,34 @@
// 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
/*const penceString = "399p";

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

console.log(`£${pounds}.${pence}`);*/

function toPounds (str) {

const penceStringWithoutTrailingP = str.substring(0,str.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("455p"))
console.log(toPounds("5p"))
console.log(toPounds("140p"))
8 changes: 8 additions & 0 deletions Sprint-2/implement/vat.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,11 @@
// Given a number,
// When I call this function with a number
// it returns the new price with VAT added on

function addVAT (num) {

return num * 1.2

}
console.log(addVAT(50))

6 changes: 6 additions & 0 deletions Sprint-2/interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,26 @@ function formatTimeDisplay(seconds) {
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

// Questions

// a) When formatTimeDisplay is called how many times will pad be called?
//3 times one for hours once for minutes once for 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?
//The first call is for hours. Since 61 seconds equals 0 hours, num = 0

// c) What is the return value of pad is called for the first time?
// num = 0 pad ensures two digits, so it returns "00"

// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
//The last call is for seconds, calculated as 61 % 60 = 1

// e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer
//The final pad call formats 1 into "01"
17 changes: 17 additions & 0 deletions Sprint-3/implement/get-angle-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,20 @@
// Identify Reflex Angles:
// When the angle is greater than 180 degrees and less than 360 degrees,
// Then the function should return "Reflex angle"


function getAngleType(num) {
if (num === 90) {
return "Right angle";
} else if (num === 180) {
return "Straight angle";
} else if (num > 0 && num < 90) {
return "Acute angle";
} else if (num > 90 && num < 180) {
return "Obtuse angle";
} else if (num > 180 && num < 360) {
return "Reflex angle";
} else {
return "Invalid number";
}
}
54 changes: 54 additions & 0 deletions Sprint-3/implement/get-angle-type.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Import the function
function getAngleType(num) {
if (num === 90) {
return "Right angle";
} else if (num === 180) {
return "Straight angle";
} else if (num > 0 && num < 90) {
return "Acute angle";
} else if (num > 90 && num < 180) {
return "Obtuse angle";
} else if (num > 180 && num < 360) {
return "Reflex angle";
} else {
return "Invalid number";
}
}





test("returns 'Acute angle' for angles between 0 and 90", function() {
expect(getAngleType(45)).toBe("Acute angle");
expect(getAngleType(89)).toBe("Acute angle");
});


test("returns 'Right angle' for 90 degrees", function() {
expect(getAngleType(90)).toBe("Right angle");
});


test("returns 'Obtuse angle' for angles between 90 and 180", function() {
expect(getAngleType(120)).toBe("Obtuse angle");
expect(getAngleType(179)).toBe("Obtuse angle");
});


test("returns 'Straight angle' for 180 degrees", function() {
expect(getAngleType(180)).toBe("Straight angle");
});


test("returns 'Reflex angle' for angles between 180 and 360",function(){
expect(getAngleType(270)).toBe("Reflex angle");
expect(getAngleType(359)).toBe("Reflex angle");
});


test("returns 'Invalid number' for numbers not in the valid range", function() {
expect(getAngleType(-1)).toBe("Invalid number");
expect(getAngleType(360)).toBe("Invalid number");
expect(getAngleType(400)).toBe("Invalid number");
});
23 changes: 23 additions & 0 deletions Sprint-3/implement/get-card-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,26 @@
// Given a card with an invalid rank (neither a number nor a recognized face card),
// When the function is called with such a card,
// Then it should throw an error indicating "Invalid card rank."

function getCardValue (card) {
const rank = card.slice(0,-1);

if (rank === "A") {
return 11;
} else if (["J", "K", "Q", "10"].includes(rank)){
return 10;
} else if(+rank >= 2 && +rank <= 9) {
return +rank;
} else {
return "Invalid card rank."
}



}

console.log(getCardValue("A♠"));
console.log(getCardValue("Q♠"));
console.log(getCardValue("4♠"));
console.log(getCardValue("$♠"));

37 changes: 37 additions & 0 deletions Sprint-3/implement/get-card-value.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
function getCardValue (card) {
const rank = card.slice(0,-1);

if (rank === "A") {
return 11;
} else if (["J", "K", "Q", "10"].includes(rank)){
return 10;
} else if(+rank >= 2 && +rank <= 9) {
return +rank;
} else {
return "Invalid card rank"
}

}

test("returns 11 for Ace", function() {
expect(getCardValue("A♠")).toBe(11)

} );

test("returns 10 for face cards (J, Q, K)", function() {
expect(getCardValue("J♠")).toBe(10);
expect(getCardValue("Q♥")).toBe(10);
expect(getCardValue("K♦")).toBe(10);
});

test("returns the numeric value for number cards (2-10)", function () {
expect(getCardValue("2♠")).toBe(2);
expect(getCardValue("10♥")).toBe(10);
expect(getCardValue("7♦")).toBe(7);
});
test("throws error for invalid card ranks", function(){
expect(getCardValue("X♠")).toBe("Invalid card rank");
expect(getCardValue("11♣")).toBe("Invalid card rank");
expect(getCardValue("#♦")).toBe("Invalid card rank");

})
15 changes: 15 additions & 0 deletions Sprint-3/implement/is-proper-fraction.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,18 @@
// target output: false
// Explanation: The fraction 3/3 is not a proper fraction because the numerator is equal to the denominator. The function should return false.
// These acceptance criteria cover a range of scenarios to ensure that the isProperFraction function handles both proper and improper fractions correctly and handles potential errors such as a zero denominator.

function isProperFraction (numerator, denominator) {

if (denominator === 0) {
return "Denominator cannot be zero" ;
} else {
return Math.abs(numerator) < Math.abs(denominator);
}
}

console.assert(isProperFraction(2, 3) === true, "Numerator can not be bigger than denominator")
console.assert(isProperFraction(5, 2) === false, "Test Case 2 Failed");
console.assert(isProperFraction(3, 0) === "Denominator cannot be zero", "Test Case 3 Failed")
console.assert(isProperFraction(-4, 7) === true, "Test Case 4 Failed");
console.assert(isProperFraction(3, 3) === false, "Test Case 5 Failed");
29 changes: 29 additions & 0 deletions Sprint-3/implement/is-proper-fraction.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
function isProperFraction (numerator, denominator) {

if (denominator === 0) {
return "Denominator cannot be zero" ;
} else {
return Math.abs(numerator) < Math.abs(denominator);
}
}


test('returns true for proper fractions (numerator < denominator)', () => {
expect(isProperFraction(2, 3)).toBe(true);
expect(isProperFraction(-2, 5)).toBe(true);
});

test('returns false for improper fractions (numerator >= denominator)', () => {
expect(isProperFraction(5, 2)).toBe(false);
expect(isProperFraction(3, 3)).toBe(false);
});

test('returns "Denominator cannot be zero" for a zero denominator', () => {
expect(isProperFraction(3, 0)).toBe("Denominator cannot be zero");
expect(isProperFraction(0, 0)).toBe("Denominator cannot be zero");
});

test('handles negative denominators correctly', () => {
expect(isProperFraction(-3, -4)).toBe(true);
expect(isProperFraction(6, -5)).toBe(false);
});
Loading