Skip to content

Commit

Permalink
feat: finished the strings exercise and reduce the size of the condit…
Browse files Browse the repository at this point in the history
…ions.js
  • Loading branch information
HakiEBIBI committed Nov 12, 2024
1 parent 71481b2 commit 481c70e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
12 changes: 2 additions & 10 deletions src/basics/conditions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/**
Expand All @@ -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

}
22 changes: 21 additions & 1 deletion src/basics/strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
}

0 comments on commit 481c70e

Please sign in to comment.