diff --git a/src/basics/conditions.js b/src/basics/conditions.js index f20d9d2..8b07ab2 100644 --- a/src/basics/conditions.js +++ b/src/basics/conditions.js @@ -18,11 +18,7 @@ export function isBiggerThan2(n) { throw new Error("Passed value is not a number"); } - if (n > 2) { - return true - } else { - return false - } + return n > 2 } /** @@ -37,10 +33,6 @@ export function isMult(n, m) { throw new Error("Passed m is not a number") } -if (m % n && n % m === 0) { - return true -} else { - return false -} +return m % n && n % m === 0 } \ No newline at end of file diff --git a/src/basics/strings.js b/src/basics/strings.js index 5c3b1ad..f4ef5ee 100644 --- a/src/basics/strings.js +++ b/src/basics/strings.js @@ -7,5 +7,25 @@ * @return {string} the resulting string, with all needle words transformed to newWord */ export function findAndReplacePreservingCase(needle, haystack, newWord) { - // Write your code here + if (typeof newWord !== 'string' || typeof needle !== 'string') { + throw new Error("not a string"); + } + + return haystack.replaceAll(new RegExp(needle, 'gi'), (match) => { + + let result = ''; + + + for (let i = 0; i < match.length; i++) { + + const newChar = newWord[i] || '' + if (match[i] === match[i].toUpperCase()) { + result += newChar.toUpperCase() + } else { + result += newChar.toLowerCase() + } + } + + return result; + }); }