From 44c5916457d76e504bff59abc63816e6b674cc88 Mon Sep 17 00:00:00 2001 From: ZabihollahNamazi Date: Mon, 9 Dec 2024 22:53:21 +0000 Subject: [PATCH 1/6] fetch --- fetch/programmer-humour/index.html | 18 ++++++++++++++++++ fetch/programmer-humour/script.js | 27 +++++++++++++++++++++++++++ fetch/programmer-humour/style.css | 4 ++++ 3 files changed, 49 insertions(+) create mode 100644 fetch/programmer-humour/index.html create mode 100644 fetch/programmer-humour/script.js create mode 100644 fetch/programmer-humour/style.css diff --git a/fetch/programmer-humour/index.html b/fetch/programmer-humour/index.html new file mode 100644 index 0000000..a9f5183 --- /dev/null +++ b/fetch/programmer-humour/index.html @@ -0,0 +1,18 @@ + + + + + + + XKCD Comic Viewer + + + + + + + + + + + \ No newline at end of file diff --git a/fetch/programmer-humour/script.js b/fetch/programmer-humour/script.js new file mode 100644 index 0000000..21e01a9 --- /dev/null +++ b/fetch/programmer-humour/script.js @@ -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() + //console.log(`${JSON.stringify(myData)} this is api`) + 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() diff --git a/fetch/programmer-humour/style.css b/fetch/programmer-humour/style.css new file mode 100644 index 0000000..2a24e61 --- /dev/null +++ b/fetch/programmer-humour/style.css @@ -0,0 +1,4 @@ +img{ + width: 500px; + height: 500px; +} \ No newline at end of file From 205c69b9044a5397e0b536062b78aefb751d8fa7 Mon Sep 17 00:00:00 2001 From: ZabihollahNamazi Date: Sat, 4 Jan 2025 21:33:36 +0000 Subject: [PATCH 2/6] calculator - step 1 and step 2 --- .../katas-tdd/calculator/calculator.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 challenges/unit-testing/katas-tdd/calculator/calculator.js diff --git a/challenges/unit-testing/katas-tdd/calculator/calculator.js b/challenges/unit-testing/katas-tdd/calculator/calculator.js new file mode 100644 index 0000000..d32197d --- /dev/null +++ b/challenges/unit-testing/katas-tdd/calculator/calculator.js @@ -0,0 +1,17 @@ +function add(numbers){ + let sum = 0; + if(numbers == ""){ + return "there is no number"; + } + let listNumber = numbers.split(","); + for(let item in listNumber){ + sum += Number(listNumber[item]); + } + return sum; +} + +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")); + From 877989efcf4098da6731e82b6589fd9e6d20271a Mon Sep 17 00:00:00 2001 From: ZabihollahNamazi Date: Sat, 4 Jan 2025 22:00:28 +0000 Subject: [PATCH 3/6] calculator - completed --- .../katas-tdd/calculator/calculator.js | 30 +++++++++++++++---- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/challenges/unit-testing/katas-tdd/calculator/calculator.js b/challenges/unit-testing/katas-tdd/calculator/calculator.js index d32197d..ea0c1dd 100644 --- a/challenges/unit-testing/katas-tdd/calculator/calculator.js +++ b/challenges/unit-testing/katas-tdd/calculator/calculator.js @@ -1,12 +1,31 @@ function add(numbers){ let sum = 0; - if(numbers == ""){ + if(numbers === ""){ return "there is no number"; } - let listNumber = numbers.split(","); - for(let item in listNumber){ - sum += Number(listNumber[item]); + 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; } @@ -14,4 +33,5 @@ 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")); \ No newline at end of file From 69f514b18eaad32af78c45b608a85caebe26a649 Mon Sep 17 00:00:00 2001 From: ZabihollahNamazi Date: Sat, 4 Jan 2025 22:28:14 +0000 Subject: [PATCH 4/6] password verifier completed --- .../password-verifier/passwverifier.js | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 challenges/unit-testing/katas-tdd/password-verifier/passwverifier.js diff --git a/challenges/unit-testing/katas-tdd/password-verifier/passwverifier.js b/challenges/unit-testing/katas-tdd/password-verifier/passwverifier.js new file mode 100644 index 0000000..3f6c4e1 --- /dev/null +++ b/challenges/unit-testing/katas-tdd/password-verifier/passwverifier.js @@ -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")); \ No newline at end of file From d2b29ea9c854447c2c56a55507fc5da9a371d5b3 Mon Sep 17 00:00:00 2001 From: ZabihollahNamazi Date: Sun, 5 Jan 2025 21:18:22 +0000 Subject: [PATCH 5/6] code formatting --- .../katas-tdd/calculator/calculator.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/challenges/unit-testing/katas-tdd/calculator/calculator.js b/challenges/unit-testing/katas-tdd/calculator/calculator.js index ea0c1dd..8ceeb8d 100644 --- a/challenges/unit-testing/katas-tdd/calculator/calculator.js +++ b/challenges/unit-testing/katas-tdd/calculator/calculator.js @@ -1,25 +1,25 @@ function add(numbers){ let sum = 0; - if(numbers === ""){ + if (numbers === ""){ return "there is no number"; } let listNumber = numbers.split(",").map(Number); let negativeNumbers = []; - for(let elem in listNumber){ - if(listNumber[elem] < 0){ + for (let elem in listNumber){ + if (listNumber[elem] < 0){ negativeNumbers.push(listNumber[elem]) } } - if(negativeNumbers.length > 0){ + if (negativeNumbers.length > 0){ throw new Error(`negatives not allowed: ${negativeNumbers.join(", ")}`); } - else{ - for(let item in listNumber){ - if(listNumber[item] > 1000){ + else { + for (let item in listNumber){ + if (listNumber[item] > 1000){ sum += 0; } - else{ + else { sum +=listNumber[item]; } From 33a12b9a75667c3c4b7c7cba97050244faee9985 Mon Sep 17 00:00:00 2001 From: ZabihollahNamazi Date: Sun, 5 Jan 2025 21:19:39 +0000 Subject: [PATCH 6/6] convert to new and old roman --- .../roman-numerals/convert-to-new-roman.js | 54 ++++++++++++++++++- .../convert-to-new-roman.test.js | 5 +- .../roman-numerals/convert-to-old-roman.js | 38 ++++++++++++- .../convert-to-old-roman.test.js | 4 +- 4 files changed, 92 insertions(+), 9 deletions(-) diff --git a/challenges/unit-testing/katas-tdd/roman-numerals/convert-to-new-roman.js b/challenges/unit-testing/katas-tdd/roman-numerals/convert-to-new-roman.js index 0f2fe4b..902cb99 100644 --- a/challenges/unit-testing/katas-tdd/roman-numerals/convert-to-new-roman.js +++ b/challenges/unit-testing/katas-tdd/roman-numerals/convert-to-new-roman.js @@ -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; diff --git a/challenges/unit-testing/katas-tdd/roman-numerals/convert-to-new-roman.test.js b/challenges/unit-testing/katas-tdd/roman-numerals/convert-to-new-roman.test.js index ae49f73..88bece8 100644 --- a/challenges/unit-testing/katas-tdd/roman-numerals/convert-to-new-roman.test.js +++ b/challenges/unit-testing/katas-tdd/roman-numerals/convert-to-new-roman.test.js @@ -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"); }); + diff --git a/challenges/unit-testing/katas-tdd/roman-numerals/convert-to-old-roman.js b/challenges/unit-testing/katas-tdd/roman-numerals/convert-to-old-roman.js index f7f0d06..42d0c71 100644 --- a/challenges/unit-testing/katas-tdd/roman-numerals/convert-to-old-roman.js +++ b/challenges/unit-testing/katas-tdd/roman-numerals/convert-to-old-roman.js @@ -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; diff --git a/challenges/unit-testing/katas-tdd/roman-numerals/convert-to-old-roman.test.js b/challenges/unit-testing/katas-tdd/roman-numerals/convert-to-old-roman.test.js index b9a4386..244fd8a 100644 --- a/challenges/unit-testing/katas-tdd/roman-numerals/convert-to-old-roman.test.js +++ b/challenges/unit-testing/katas-tdd/roman-numerals/convert-to-old-roman.test.js @@ -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"); });