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..8ceeb8d --- /dev/null +++ b/challenges/unit-testing/katas-tdd/calculator/calculator.js @@ -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; +} + +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 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 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"); }); 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