Skip to content

Commit c147742

Browse files
committed
added algorithms JS
1 parent 5963109 commit c147742

23 files changed

+504
-0
lines changed

JS/2_algorithms/0_bubbleSortUp.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
let arr = [12, 2, 45, 6, 32, 357, 24, 6346, 123, 75];
2+
3+
function bubbleSort(arr) {
4+
for (let i = 0; i < arr.length; i++) {
5+
for (let j = 0; j < arr.length - 1; j++) {
6+
if (arr[j + 1] > arr[j]) {
7+
let temp = arr[j];
8+
arr[j] = arr[j + 1];
9+
arr[j + 1] = temp;
10+
}
11+
}
12+
}
13+
return arr;
14+
}
15+
16+
console.log(bubbleSort(arr));
17+

JS/2_algorithms/10_capitalization.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function capitalization(str) {
2+
const arr = str.split(" ").map(word => {
3+
word = word.split("");
4+
word[0] = word[0].toUpperCase();
5+
return word.join("");
6+
});
7+
8+
return arr.join(" ");
9+
}
10+
11+
console.log(capitalization("a short sentence"));
12+
console.log(capitalization("a lazy fox"));
13+
console.log(capitalization("look, it is working!"));
14+

JS/2_algorithms/11_findWord.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
let paragraph = "Bob hit a ball, the hit BALL flew far after it was hit";
2+
3+
function fillArray(paragraph) {
4+
let regexp = /,/;
5+
let freq = {};
6+
let max = 0;
7+
8+
paragraph = paragraph
9+
.replace(regexp, "")
10+
.toLowerCase()
11+
.split(" ");
12+
13+
paragraph.forEach(word => {
14+
if (freq[word]) {
15+
freq[word]++;
16+
} else {
17+
freq[word] = 1;
18+
}
19+
20+
if (freq[word] > max) {
21+
max = freq[word];
22+
result = word;
23+
}
24+
});
25+
26+
return result;
27+
}
28+
29+
console.log(fillArray(paragraph)); // hit

JS/2_algorithms/12_fizzBuzz.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function fizzBuzz(n) {
2+
for (let i = 1; i <= n; i++) {
3+
str = "";
4+
if (i % 3 === 0) str += "fizz";
5+
if (i % 5 === 0) str += "buzz";
6+
7+
console.log(str ? str : i);
8+
}
9+
}
10+
11+
fizzBuzz(30);
12+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
let testCase1 = "()";
2+
let testCase2 = "()[]{}";
3+
let testCase3 = "(|";
4+
let testCase4 = "(|)]";
5+
let testCase5 = "{[]}";
6+
7+
function isCorrect(str) {
8+
let stack = [];
9+
for (let i = 0; i < str.length; i++) {
10+
if (str[i] === "[" || str[i] === "(" || str[i] === "{") {
11+
stack.push(str[i]);
12+
} else {
13+
if (stack.length === 0) {
14+
return false;
15+
}
16+
let top = stack.pop();
17+
if (top === "[" && str[i] !== "]") return false;
18+
if (top === "(" && str[i] !== ")") return false;
19+
if (top === "{" && str[i] !== "}") return false;
20+
}
21+
}
22+
return stack.length === 0 ? true : false;
23+
}
24+
25+
console.log(isCorrect(testCase1)); // true
26+
console.log(isCorrect(testCase2)); // true
27+
console.log(isCorrect(testCase3)); // false
28+
console.log(isCorrect(testCase4)); // false
29+
console.log(isCorrect(testCase5)); // true
30+

JS/2_algorithms/14_isPalindrom.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
let testCase1 = "abba";
2+
let testCase2 = "a Bb a";
3+
let testCase3 = "bba";
4+
let testCase4 = "aba";
5+
6+
function isPalindrom(s) {
7+
let regex = /\s/g;
8+
9+
s = s.replace(regex, "").toLowerCase();
10+
11+
for (let i = 0; i < s.length / 2; i++) {
12+
if (s[i] !== s[s.length - 1 - i]) {
13+
return false;
14+
}
15+
}
16+
return true;
17+
}
18+
19+
console.log(isPalindrom(testCase1)); // true
20+
console.log(isPalindrom(testCase2)); // true
21+
console.log(isPalindrom(testCase3)); // false
22+
console.log(isPalindrom(testCase4)); // true
23+

JS/2_algorithms/15_mostCommonChar.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const str = "abcccdd";
2+
3+
function maxChar(str) {
4+
const obj = {};
5+
let max = 0;
6+
let result = "";
7+
8+
for (let char of str) {
9+
obj[char] = obj[char] + 1 || 1;
10+
if (obj[char] > max) {
11+
max = obj[char];
12+
result = char;
13+
}
14+
}
15+
16+
return result;
17+
}
18+
19+
console.log(maxChar(str));
20+

JS/2_algorithms/16_myOwnForEach.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Array.prototype.myForEach = function(func) {
2+
for (var i = 0; i < this.length; i++) {
3+
func(this[i]);
4+
}
5+
};
6+
7+
[1, 2, 3, 45].myForEach(i => {
8+
console.log(i);
9+
});
10+

JS/2_algorithms/17_palindrom.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const word = "abba";
2+
3+
function first(word) {
4+
const length = word.length - 1;
5+
6+
for (let i = 0; i <= length / 2; i++) {
7+
if (word[i] !== word[length - i]) return false;
8+
}
9+
return true;
10+
}
11+
12+
function second(word) {
13+
const reversed = word
14+
.split("")
15+
.reverse()
16+
.join("");
17+
18+
return reversed === word;
19+
}
20+
21+
console.log(first(word));
22+
console.log(second(word));
23+

JS/2_algorithms/18_pyramid.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function pyramid(n) {
2+
const mid = Math.floor(n - 0.5);
3+
4+
for (let i = 0; i < n; i++) {
5+
let line = "";
6+
7+
for (let j = 0; j <= 2 * n - 1; j++) {
8+
if (mid - i <= j && mid + i >= j) {
9+
line += "#";
10+
} else {
11+
line += " ";
12+
}
13+
}
14+
console.log(line, line.length);
15+
}
16+
}
17+
18+
console.log(pyramid(4));
19+

0 commit comments

Comments
 (0)