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

Manchester | Zabihollah Namazi | Module-Data-Flows | Challenges | unit-testing | katas-tdd #122

Open
wants to merge 6 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
37 changes: 37 additions & 0 deletions challenges/unit-testing/katas-tdd/calculator/calculator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
function add(numbers){
let sum = 0;
if (numbers === ""){
return "there is no number";
}
let listNumber = numbers.split(",").map(Number);

let negativeNumbers = [];
for (let elem in listNumber){
if (listNumber[elem] < 0){
negativeNumbers.push(listNumber[elem])
}
}
if (negativeNumbers.length > 0){
throw new Error(`negatives not allowed: ${negativeNumbers.join(", ")}`);
}
else {
for (let item in listNumber){
if (listNumber[item] > 1000){
sum += 0;
}
else {
sum +=listNumber[item];
}

}
}

return sum;
Copy link

Choose a reason for hiding this comment

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

Overall looks good. Please, mind code formatting and spacing:

Bad:

){
if(
else{
=listNumber

Good:

) {
if {
else {
= listNumber

Copy link
Author

Choose a reason for hiding this comment

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

thanks

}

console.log(add(""));
console.log(add("3"));
console.log(add("3,2,4,1"));
console.log(add("3,2,4,1,33,45,12,56,78, 34"));
console.log(add("2,3,1001"));
console.log(add("3,2,4,1,-11,-4"));
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function verify(password){
if(password == null || password.length < 8){
return "Password rejected";
}
else if(!/[A-Z]/.test(password)){
return "Password rejected";
}
else if(!/[0-9]/.test(password)){
return "Password rejected";
}
else{
return 'Password accepted';
}
}

console.log(verify("ej"));
console.log(verify(null));
console.log(verify("helloworld"));
console.log(verify("HelloWorld"));
console.log(verify("HelloWorld1"));
console.log(verify("HELLOWORLD223"));
Original file line number Diff line number Diff line change
@@ -1,3 +1,55 @@
function convertToNewRoman(n) {}
function convertToNewRoman(n) {
let roman = "";

// Thousands place (1000)
while (n >= 1000) {
roman += "M";
n -= 1000;
}

// Hundreds place (500)
while (n >= 500) {
roman += "D";
n -= 500;
}

// Hundreds place (100)
while (n >= 100) {
roman += "C";
n -= 100;
}

// Tens place (50)
while (n >= 50) {
roman += "L";
n -= 50;
}

// Tens place (10)
while (n >= 10) {
roman += "X";
n -= 10;
}

// Ones place (5)
while (n >= 5) {
roman += "V";
n -= 5;
}

// Ones place (1)
while (n >= 1) {
roman += "I";
n -= 1;
}

return roman;
}

console.log(convertToNewRoman(4));
console.log(convertToNewRoman(77));
console.log(convertToNewRoman(1));



module.exports = convertToNewRoman;
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
let convertToNewRoman = require("./convert-to-new-roman");

test("returns I if passed 1 as an argument", function () {
// Arrange
// Act
// Assert
expect(convertToNewRoman(1)).toBe("I");
});

Original file line number Diff line number Diff line change
@@ -1,3 +1,37 @@
function convertToOldRoman(n) {}

function convertToOldRoman(num) {
const romanMapping = [
{ value: 1000, numeral: "M" },
{ value: 900, numeral: "CM" },
{ value: 500, numeral: "D" },
{ value: 400, numeral: "CD" },
{ value: 100, numeral: "C" },
{ value: 90, numeral: "XC" },
{ value: 50, numeral: "L" },
{ value: 40, numeral: "XL" },
{ value: 10, numeral: "X" },
{ value: 9, numeral: "IX" },
{ value: 5, numeral: "V" },
{ value: 4, numeral: "IV" },
{ value: 1, numeral: "I" }
];

let roman = "";

for (const { value, numeral } of romanMapping) {
// Append the numeral as many times as the value fits into num
while (num >= value) {
roman += numeral;
num -= value;
}
}

return roman;
}

console.log(convertToOldRoman(4));
console.log(convertToOldRoman(9));
console.log(convertToOldRoman(58));
console.log(convertToOldRoman(1999));
console.log(convertToOldRoman(3000));
console.log(convertToOldRoman(14));
module.exports = convertToOldRoman;
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
let convertToOldRoman = require("./convert-to-old-roman");

test("returns I if passed 1 as an argument", function () {
// Arrange
// Act
// Assert
expect(convertToOldRoman(14)).toBe("XIV");
});
18 changes: 18 additions & 0 deletions fetch/programmer-humour/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>XKCD Comic Viewer</title>
<script src="script.js" defer></script>
</head>
<body>
<!-- <section id="section"></section> -->





</body>
</html>
27 changes: 27 additions & 0 deletions fetch/programmer-humour/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

let myData;

async function fetchData() {
try{
const response = await fetch("https://xkcd.now.sh/?comic=latest")

if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}

myData = await response.json()
Copy link

Choose a reason for hiding this comment

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

Don't forget to add ; at the end of your expressions. JS allows to omit them, but this can lead to unexpected errors/results.

Copy link
Author

Choose a reason for hiding this comment

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

added

//console.log(`${JSON.stringify(myData)} this is api`)
Copy link

Choose a reason for hiding this comment

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

Please, remove commented out code from your final work.

Copy link
Author

Choose a reason for hiding this comment

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

removed

let section = document.body;
const image = document.createElement("img");
image.src = myData.img;
image.alt = myData.alt;
section.appendChild(image);
}
catch(error) {
console.error("an error happened", error.message);
alert("Something went wrong. Please try again later.");
}

}

fetchData()
4 changes: 4 additions & 0 deletions fetch/programmer-humour/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
img{
width: 500px;
height: 500px;
}