Skip to content

Commit 78bac70

Browse files
committed
new programs are added
1 parent 235d38b commit 78bac70

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

check_anagrams.js

+15
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,18 @@ function areAnagrams(str1, str2) {
2121
console.log(areAnagrams("listen", "silent"));
2222
console.log(areAnagrams("hello", "world"));
2323
console.log(areAnagrams("mest", "stem"));
24+
25+
function areAnagram(str1, str2) {
26+
const normalize = (str) =>
27+
str
28+
.toLowerCase()
29+
.replace(/[^a-z0-9]/g, "")
30+
.split("")
31+
.sort()
32+
.join("");
33+
return normalize(str1) === normalize(str2);
34+
}
35+
36+
// Example usage:
37+
console.log(areAnagram("listen", "silent"));
38+
console.log(areAnagram("hello", "world"));

factorial.js

+10
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,13 @@ const find_factorial = (num) => {
1010

1111
const res = find_factorial(number);
1212
console.log(res);
13+
14+
15+
function factorial(n) {
16+
if (n === 0) return 1;
17+
return n * factorial(n - 1);
18+
}
19+
20+
// Example usage:
21+
console.log(factorial(5)); // Output: 120
22+

longest_word.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function findLongestWord(sentence) {
2+
let words = [];
3+
let word = "";
4+
5+
for (let i = 0; i < sentence.length; i++) {
6+
if (sentence[i] === " ") {
7+
words.push(word);
8+
word = "";
9+
} else {
10+
word += sentence[i];
11+
}
12+
}
13+
words.push(word); // Add the last word
14+
15+
let longestWord = words[0];
16+
for (let i = 1; i < words.length; i++) {
17+
if (words[i].length >= longestWord.length) {
18+
longestWord = words[i];
19+
}
20+
}
21+
22+
return longestWord;
23+
}
24+
25+
// Example usage:
26+
console.log(findLongestWord("The quickheal brown fox jumps overlacud the lazy dog")); // Output: 'jumps'

0 commit comments

Comments
 (0)