Skip to content
Closed
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
5 changes: 3 additions & 2 deletions Sprint-2/debug/0.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Predict and explain first...

// Predict 320
//undefined because multiply function has to get return
function multiply(a, b) {
console.log(a * b);
return a * b;
}

console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
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...

// Predict error because a+b is another line(undefined)
function sum(a, b) {
return;
a + b;
// return;
return a + b;
}

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

// Predict error function is calling the number of 103 anc slice to 103 and the result can be 3
//if i add parameter to getLastDigit it will work
const num = 103;

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

Expand Down
8 changes: 6 additions & 2 deletions Sprint-2/errors/0.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
// Predict and explain first...
//predict i think the error is on the Parameter for capitalise is undefined
//read is str has already been declared so that mean function parameter can not replace

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

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

console.log(capitalise("mgphone"));
7 changes: 4 additions & 3 deletions Sprint-2/errors/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// Predict and explain first...
//error because decimalNumber is redeclare and wrong calling function

// Why will an error occur when this program runs?
// Try playing computer with the example to work out what is going on

// i did call the convertToPercentage with parameter
function convertToPercentage(decimalNumber) {
const decimalNumber = 0.5;
// decimalNumber = 0.5;
const percentage = `${decimalNumber * 100}%`;

return percentage;
}

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

// Predict and explain first...
//error because there is function is unfinished, calling is wrong

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

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


console.log(square(3));
31 changes: 29 additions & 2 deletions Sprint-2/extend/format-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,38 @@
// 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.

function formatAs12HourClock(time) {
const hours = Number(time.slice(0, 2));
if (!time || time.length !== 5) {
return "Invalid Number";
}
const splitTime = time.split(":");

if (splitTime.length !== 2) {
return "Invalid format time";
}

const hours = Number(splitTime[0]);
const minutes = +splitTime[1];
if (
isNaN(hours) ||
isNaN(minutes) ||
minutes < 0 ||
minutes > 59 ||
hours < 0 ||
hours > 23
) {
return "Invalid Time value";
}

if (hours > 12) {
return `${hours - 12}:00 pm`;
}
return `${time} am`;
if (hours == 0) {
return `12:${minutes} am`;
}
if (hours == 12) {
return `12:${minutes} pm`;
}
return `${hours}:${minutes} am`;
}

const currentOutput = formatAs12HourClock("08:00");
Expand Down
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 bmiCalculator(weightInKg, tallInMeter) {
const bmi = weightInKg / (tallInMeter * tallInMeter);
return bmi.toFixed(1);
}
console.log(bmiCalculator(70, 1.73));
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 changeUpperSnakeCase(inputString) {
let changeSpaceToUnderScore = inputString.replaceAll(" ", "_");
let changeUpperCase = changeSpaceToUnderScore.toUpperCase();
return changeUpperCase;
}
console.log(changeUpperSnakeCase("lord of the rings"));
20 changes: 20 additions & 0 deletions Sprint-2/implement/to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,23 @@
// 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";

function changeToPound(input) {
const penceStringWithoutTrailingP = input.substring(0, input.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(changeToPound("399p"));
test("abcdef", () => {
expect(3).toBe("Only Positive Integer");
});
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 addTax(input) {
const withVat = input * 1.2;
return withVat.toFixed(2);
}
console.log(addTax(50));
13 changes: 6 additions & 7 deletions Sprint-2/interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ function formatTimeDisplay(seconds) {
const remainingMinutes = totalMinutes % 60;
const totalHours = (totalMinutes - remainingMinutes) / 60;

return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(
remainingSeconds
)}`;
return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`;
}

// You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit
Expand All @@ -19,13 +17,14 @@ function formatTimeDisplay(seconds) {
// Questions

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

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

// the value will be 1 because it have already been used to left and right of pad and that is only remainingSeconds is 1
// e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer
// return value of last pad is 01 because remainder of 61 is 1 and add the padStart
15 changes: 15 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,18 @@
// 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(degrees) {
if (degrees < 90 && degrees > 0) {
return "Actue Angle";
} else if (degrees == 90) {
return "Right Angles";
} else if (degrees > 90 && degrees < 180) {
return "Obtuse Angle";
} else if (degrees == 180) {
return "Straight Angle";
} else if (degrees > 180 && degrees < 360) {
return "Reflex Angle";
} else return "Your angle should not be less than 0 or greater than 360";
}
console.log(getAngleType(0));
module.exports = getAngleType;
45 changes: 45 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,48 @@
// 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(checkingCard, emoji) {
// // const splitCard = checkingCard.split("");
// // return splitCard;
// const emojiArray = ["❤️", "♠️", "♦️", "♣️"];
// const isEmojiValid = emojiArray.includes(emoji);
// const valueOfCard =
// checkingCard == "A"
// ? 11
// : checkingCard == "K" || checkingCard == "Q" || checkingCard == "J"
// ? 10
// : !isNaN(checkingCard) && checkingCard >= 2 && checkingCard <= 10
// ? Number(checkingCard)
// : false;
// return isEmojiValid && valueOfCard ? valueOfCard : "Invalid card rank";
// }

// console.log(getCardValue("A", "♠️"));
function getCardValue(card) {
const validSuit = ["❤️", "♠️", "♦️", "♣️"];

const rank = card.slice(0, -2);
const suit = card.slice(-2);
// console.log("this is rank" + rank);
// console.log("this is suit" + suit);

if (!validSuit.includes(suit)) {
throw new Error("Invalid card suit");
}
const valueOfCard =
rank === "A"
? 11
: ["K", "Q", "J", "10"].includes(rank)
? 10
: !isNaN(rank) && rank >= 2 && rank <= 9
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do you decide to treat "02❤️", "002❤️", and "0x02❤️" the same as "2❤️", but does treat "010❤️" the same as "10❤️"?

Copy link
Author

Choose a reason for hiding this comment

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

yeah i did check and all have got same value :)

? Number(rank)
: false;
if (!valueOfCard) {
throw new Error("Invalid card rank");
}
return valueOfCard;
}
// console.log(getCardValue("LL"));
// console.log(getCardValue("002♦️"));

module.exports = getCardValue;
8 changes: 8 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,11 @@
// 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.
const isProperFraction = (numerator, denominator) => {
if (denominator === 0) {
throw new Error("Denominator cannot be zero");
}
return Math.abs(numerator) < Math.abs(denominator);
};
// console.log(isProperFraction(2, 1));
module.exports = isProperFraction;
6 changes: 6 additions & 0 deletions Sprint-3/implement/is-valid-triangle.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,9 @@
// Then it should return true because the input forms a valid triangle.

// This specification outlines the behavior of the isValidTriangle function for different input scenarios, ensuring it properly checks for invalid side lengths and whether they form a valid triangle according to the Triangle Inequality Theorem.
function isValidTriangle(a, b, c) {
return a > 0 && b > 0 && c > 0 && a + b > c && b + c > a && a + c > b;
}
console.log(isValidTriangle(0, 2, 1));

module.exports = isValidTriangle;

Choose a reason for hiding this comment

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

The code does not check if any of the sides are less than or equal to zero.

Copy link
Author

Choose a reason for hiding this comment

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

Please guide me, when i check jest all are correct...
test("Is a valid triangle", () => {
expect(isValidTriangle(3, 3, 3)).toBe(true);
});

test("Invalid Triangle with Negative", () => {
expect(isValidTriangle(1, -2, 1)).toBe(false);
});

test("Invalid checking triangle with zero sides", () => {
expect(isValidTriangle(0, 1, 2)).toBe(false);
});
test("checking invalid", () => {
expect(isValidTriangle(1, 2, 3)).toBe(false);
});

Copy link
Author

Choose a reason for hiding this comment

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

return a + b > c && b + c > a && a + c > b;
to make it clear you want me to check if any parameter is zero or minus make it return to false?

Copy link
Contributor

Choose a reason for hiding this comment

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

@MonaZaresh
Yesterday, while trying to find some non-positive values that could fail the test a + b > c && b + c > a && a + c > b, I realised that in JS checking if a, b, c are all positives is not needed. This is because JS uses only double precision floating point type to represent numbers, so we don't have to worry about "integer overflow".

In other programming languages that support integer types, if a, b, c are integer types, then we would need to check if a, b, c are all positives in addition to checking a + b > c && b + c > a && a + c > b.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks, that mean my code for javascript is correct right :)

Loading