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

northwest |Rahwa Zeslus |module-structure-and-Testing-Data | WEEK3 #224

Open
wants to merge 31 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
fe5ab9e
sprint2Exercises
RahwaZeslusHaile Oct 29, 2024
dc58894
the function to identify the angle
RahwaZeslusHaile Nov 17, 2024
89ced3f
a java script code to identify the validity of function
RahwaZeslusHaile Nov 24, 2024
f1d08ce
writing different assertion for a function getCardValue
RahwaZeslusHaile Nov 25, 2024
051dde0
a function that identify the validness of triangle
RahwaZeslusHaile Nov 25, 2024
2596718
a function that shifts value and character as input
RahwaZeslusHaile Nov 27, 2024
dede04f
updated
RahwaZeslusHaile Dec 9, 2024
4d3a429
updated
RahwaZeslusHaile Dec 9, 2024
81f1dc7
Explanation reason for the bug with the correct code
RahwaZeslusHaile Dec 14, 2024
968ebe1
correct BMI calculation
RahwaZeslusHaile Dec 14, 2024
00be24d
Making the function more descriptive
RahwaZeslusHaile Dec 14, 2024
a31b23f
handling edge cases
RahwaZeslusHaile Dec 14, 2024
c8e6bb1
Proper Declaration
RahwaZeslusHaile Dec 14, 2024
e551597
A function and a test to count the char
RahwaZeslusHaile Dec 15, 2024
7b89028
Function to get ordinary number
RahwaZeslusHaile Dec 15, 2024
b9c04de
different file name for js and test.js
RahwaZeslusHaile Dec 16, 2024
b482ac3
A function to get ordinal number with test
RahwaZeslusHaile Dec 16, 2024
2f1cb35
A function to check if the given number is prime or not
RahwaZeslusHaile Dec 16, 2024
6bf3fbb
a test for ordinal number
RahwaZeslusHaile Dec 16, 2024
1f64f33
A function to check the password validation
RahwaZeslusHaile Dec 16, 2024
0842687
A function and test to repeat the string
RahwaZeslusHaile Dec 16, 2024
7a67c3b
while loop statement
RahwaZeslusHaile Dec 16, 2024
9099e59
credit card validator
RahwaZeslusHaile Dec 16, 2024
0e8952c
A function to get an Angle Type
RahwaZeslusHaile Jan 4, 2025
a128541
get-card-value
RahwaZeslusHaile Jan 13, 2025
8f9ab5d
Updated-is-valid-triangle function
RahwaZeslusHaile Jan 13, 2025
a05fdef
Updated-rotate-char
RahwaZeslusHaile Jan 14, 2025
fe69f36
Updated card-validator
RahwaZeslusHaile Jan 21, 2025
f964082
Updated Is-prime-number
RahwaZeslusHaile Jan 21, 2025
8f7c9aa
Updated password-validator test by adding a lowercase letter to make …
RahwaZeslusHaile Jan 21, 2025
6a501ab
Updating long code to single string.repeat(count) method
RahwaZeslusHaile Jan 21, 2025
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: 2 additions & 2 deletions Sprint-2/debug/0.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Predict and explain first...

// in the function multiply it should be returning the value instead of logging ,so it will be undefined.
function multiply(a, b) {
console.log(a * b);
return(a * b);
}

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

//syntax error :the error is the semicolon and sum is place on new line from the return ,which does not have to execute in returning the result
function sum(a, b) {
return;
a + b;
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...
// the num is already declared by the constant keyword ,which is not going to change in every logging.
Copy link

Choose a reason for hiding this comment

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

While your implementation is correct, your explanation does not clearly explain the reason for the bug. Please give your code a second look and update your explanation.


const num = 103;

function getLastDigit() {

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

Expand Down
9 changes: 6 additions & 3 deletions Sprint-2/errors/0.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
// Predict and explain first...

//the str is occured twice in the function and declaration
//
// call the function capitalise with a string input

// interpret the error message and figure out why an error is occurring
//SyntaxError: Identifier 'str' has already been declared :this means str is a parameter in the capitalise function and redeclare by keyword let which is not allowed to redeclare by let keyword

function capitalise(str) {
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
}
18 changes: 9 additions & 9 deletions Sprint-2/errors/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Predict and explain first...
//decimalNumber is a parameter in the convertToPercentage function and it is declared again by const keyword.

// 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);
//SyntaxError: Identifier 'decimalNumber' has already been declared: decimalNumber is declared as parameter in the function,and it is also assigned to the number by the keyword const.

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

return percentage;}

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

// Predict and explain first...

//the number 3 as a parametre in square function is invalid
// this function should square any number but instead we're going to get an error

function square(3) {
//SyntaxError: Unexpected number
function square(num) {
return num * num;
}
console.log(square(3));


7 changes: 7 additions & 0 deletions Sprint-2/implement/bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,10 @@
// 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 BMICalculation(height,adultWeight){
const heightSquare= Math.sqrt(height);
Copy link

Choose a reason for hiding this comment

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

Your BMI implementation is wrong. You are doing something incorrectly with your height. Please look up the correct formula for BMI calculation.

const BMI = adultWeight/heightSquare
return BMI.toFixed(1)
}

console.log(BMICalculation(1.5 , 60))
5 changes: 5 additions & 0 deletions Sprint-2/implement/cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@

// You will need to come up with an appropriate name for the function
// Use the string documentation to help you find a solution
function changeToUpper(words){
Copy link

Choose a reason for hiding this comment

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

Can you come up with a more descriptive name for this function? changeToUpper does not clearly describe what the function is doing

let upperWords = words.split(" ").join('_').toUpperCase();
return upperWords;
}
console.log(changeToUpper("lord of the rings"));
23 changes: 23 additions & 0 deletions Sprint-2/implement/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("90P"))

Copy link

Choose a reason for hiding this comment

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

It would be best if you also handled edge cases like empty strings, missing "P", or invalid input.


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 priceWithoutVAT(price){
priceWithVAT=price+ (price*0.2);
Copy link

Choose a reason for hiding this comment

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

Can you make sure priceWithVAT is properly declared

return priceWithVAT;
}
console.log (priceWithoutVAT(50));
11 changes: 8 additions & 3 deletions Sprint-2/interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,18 @@ function formatTimeDisplay(seconds) {
// Questions

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

// three times:totalHours,remainingMinutes and remainingSeconds
// 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?

//remainingSeconds = 61%60 = 1
//totalMinutes = (61 - 1)/60 = 1
//remainingMinutes = 1 % 60 = 1
//totalHours = (1 - 1)/60 = 0
// c) What is the return value of pad is called for the first time?
//00:01:01

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

// remainingSeconds: 1
// e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer
// num.toString().padStart(2, "0"); 01
23 changes: 23 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,26 @@
// 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(angleMeasure){

if (angleMeasure === 90){
return "Right_Angle";
}
else if (angleMeasure < 90) {
return "Acute angle";
}
else if (angleMeasure > 90 && angleMeasure < 180) {
return "Obtuse angle";
}
else if (angleMeasure===180) {
return "Straight angle";
}
else if (angleMeasure > 180) {
return "Reflex angle";
}
}
Copy link

Choose a reason for hiding this comment

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

What do you expect from the following function calls?

getAngleType(360)
getAngleType(1000)
getAngleType(0)
getAngleType(-1000)

If the spec is not clear about how to classify 0 or negative angles, you can lookup the definition of "Acute angle".

Copy link
Author

Choose a reason for hiding this comment

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

Yes I understand ,I didn't see that way. So what I have done in this new code is try to change the degrees to to an equivalent angle within the standard range of
0 degree to 360 degree

console.log(getAngleType(90))
console.log(getAngleType(110))
console.log(getAngleType(10))
console.log(getAngleType(180))
console.log(getAngleType(200))
Copy link

Choose a reason for hiding this comment

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

Do you notice any inconsistency among the output produced by these statements?

24 changes: 24 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,27 @@
// 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);
// to exclude the last character

// Check for Number Cards (2-10)
if (!isNaN(rank) && Number(rank) >= 2 && Number(rank) <= 10) {
return Number(rank);
}
Comment on lines +39 to +41
Copy link

Choose a reason for hiding this comment

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

What do you expect from the following function calls?
Does your function return the value you expected?

getCardValue("010♠");
getCardValue("02♠");
getCardValue("0x02♠");
getCardValue("2.1♠")

Copy link
Author

@RahwaZeslusHaile RahwaZeslusHaile Jan 13, 2025

Choose a reason for hiding this comment

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

1,getCardValue("010♠"); expected : 10
!isNaN(rank) → true because "010" is a numeric string.
Number(rank) → 10 (leading zeros are ignored by Number()).
Number(rank) >= 2 && Number(rank) <= 10 → true.

2,getCardValue("02♠"); expected :2

!isNaN(rank) → true because "02" is a numeric string.
Number(rank) → 2 (leading zeros are ignored by Number()).
Number(rank) >= 2 && Number(rank) <= 10 → true.

3,getCardValue("0x02♠"); Expected output: 2

!isNaN(rank) → true because "0x02" is interpreted as a valid hexadecimal number in JavaScript.
Number(rank) → 2 (JavaScript converts "0x02" to its decimal equivalent).
Number(rank) >= 2 && Number(rank) <= 10 → true.
Expected output: 2.

4,getCardValue("2.1♠") ;Expected output: 2.1
!isNaN(rank) → true because "2.1" is a valid floating-point number.
Number(rank) → 2.1.
Number(rank) >= 2 && Number(rank) <= 10 → true.

Copy link

Choose a reason for hiding this comment

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

At the end, do you want the function to recognise these card values?


// Check for Face Cards (J, Q, K) or '10'
if (['J', 'Q', 'K'].includes(rank) || rank === '10') {
return 10;
}

// Check for Ace (A)
if (rank === 'A') {
return 11;
}


throw new Error("Invalid card rank.");
}
15 changes: 14 additions & 1 deletion Sprint-3/implement/is-proper-fraction.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// You wil need to implement a function isProperFraction
// You wil need to implement a function isProperFraction
// You need to write assertions for your function to check it works in different cases

// Terms:
Expand Down Expand Up @@ -32,3 +32,16 @@
// 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) {
throw new Error("Denominator cannot be zero; it's not a valid fraction.");
}

if (Math.abs(numerator) < Math.abs(denominator)) {
return true;
}

return false;
}
21 changes: 21 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,24 @@
// 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.


//here I have to use a function with three parameter a,b,c
function isValidTriangle(a,b,c){
// Another way to write this is a + b > c
// It's also true that b + c > a
// It's also true that a + c > b
if((a + b > c) && (a + c > b) && (b + c > a)){
return true;
}
// scenario: invalid triangle
// Check for Valid Input:
// Given the sides a, b, and c,
// When any of the sides are less than or equal to zero,
// Then it should return false because a triangle cannot have zero or negative side lengths.
if(a <= 0 || b <= 0 || c <= 0){
return false;
}
Copy link

Choose a reason for hiding this comment

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

If any of a, b, and c is less than or equal to zero, then the condition at line 43 will always be false.
Is there any need to further check if a, b, and c is less than or equal to zero at line 51?

I will not go into details why in some programming languages (but not JavaScript) we need also to ensure a, b, c are positives.

Copy link
Author

Choose a reason for hiding this comment

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

Thank you for your review! You're absolutely right in your observation. Since the check for whether any side is less than or equal to zero is already handled in the first condition (a <= 0 || b <= 0 || c <= 0) at line 43, the second check at line 51 is unnecessary.

This means the function can be simplified by removing that second check, making the logic more concise and clearer.

// if any other condition comes it return false
return false;
}
22 changes: 22 additions & 0 deletions Sprint-3/implement/rotate-char.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,36 @@
// Scenario: Rotate Lowercase Letters:
// Given a lowercase letter character and a positive integer shift,
// When the function is called with these inputs,

// Then it should rotate the lowercase letter by shift positions within the lowercase alphabet, wrapping around if necessary, and return the rotated lowercase letter as a string.
function rotateCharacter(char, shift) {
// Check if the character is a lowercase letter
if (char >= "a" && char <= "z") {
// Calculate new character with wraparound
const charCode = char.charCodeAt(0);
const rotatedCode = ((charCode - 97 + shift) % 26) + 97; // 'a' starts at ASCII 97
Copy link

Choose a reason for hiding this comment

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

This approach is good.
If shift were allowed to be negative so that charCode - 97 + shift can become a negative number, how would you modify your code to deal with negative shift?

return String.fromCharCode(rotatedCode);
}


console.log(rotateCharacter("a", 3)); // Output: "d"
console.log(rotateCharacter("f", 1)); // Output: "g"

// Scenario: Rotate Uppercase Letters:
// Given an uppercase letter character and a positive integer shift,
// When the function is called with these inputs,
// Then it should rotate the uppercase letter by shift positions within the uppercase alphabet, wrapping around if necessary, and return the rotated uppercase letter as a string.
if (char >= "A" && char <= "Z") {
// Calculate new character with wraparound
const charCode = char.charCodeAt(0);
const rotatedCode = ((charCode - 65 + shift) % 26) + 65; // 'A' starts at ASCII 65
return String.fromCharCode(rotatedCode);
}

// If it's not a letter, return the character unchanged
return char;
}

console.log(rotateCharacter("A", 3)); // Output: "D"
console.log(rotateCharacter("F", 1)); // Output: "G"

Expand Down