Skip to content

Commit 03375f4

Browse files
committed
Initial commit
0 parents  commit 03375f4

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

‎Practice.js‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* Returns the number of times a character occurs in a string */
2+
function numCount(arr, element) {
3+
// numCount("hello, l") return 2l
4+
var count = 0;
5+
for (var i = 0; i<arr.length; i++){
6+
if (arr[i]===element) {
7+
count++
8+
}
9+
}
10+
return `${count}${element}`;
11+
}
12+
13+
/* Uses numCount function to return run length encoding of all characters in a string */
14+
function runLengthEncoding(str) {
15+
// runLengthEncoding("Hello World") returns 1h1e3l2o1w1r1d
16+
var stringArray = str.toLowerCase().replace(/[^a-z]/g,"").split("");
17+
var uniqueStringArray = [... new Set(stringArray)]
18+
return uniqueStringArray.map((a) => numCount(stringArray, a)).join("");
19+
}
20+
21+
/* Reverses words in a sentence */
22+
function reverseSentence(str) {
23+
// reverseSentence("Hello World") returns olleH dlroW
24+
return str.split(" ").map(a => a.split("").reverse().join("")).join(" ");
25+
}
26+
27+
/* Determines if a string is a pangram or not */
28+
function pangram(str) {
29+
// If a set of the string has exactly 26 letters which are all the letters of the English alphabet, it's a pangram
30+
const stringArray = str.toLowerCase().replace(/[^a-z]/g,"").split("");
31+
const uniqueLetters = [... new Set(stringArray)];
32+
return uniqueLetters.length === 26 ? "Pangram" : "Not pangram"
33+
}

0 commit comments

Comments
 (0)