Skip to content

Commit a9beefb

Browse files
committed
elementary algorithm JavaScript solutions added
1 parent 675f694 commit a9beefb

28 files changed

+555
-0
lines changed

elementaryAlgo/alternateCap.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Write a function `alternatingCaps` that accepts a sentence string as an argument. The function should
2+
// return the sentence where words alternate between lowercase and uppercase.
3+
const alternatingCaps = (words) => {
4+
let result = "";
5+
let arraySplit = words.split(" ");
6+
for (let i = 0; i < arraySplit.length; i++) {
7+
let word = arraySplit[i];
8+
if (i % 2 === 0) {
9+
result += word.toLowerCase() + " ";
10+
} else {
11+
result += word.toUpperCase() + " ";
12+
}
13+
}
14+
return result;
15+
};
16+
console.log(alternatingCaps("take them to school")); // 'take THEM to SCHOOL'
17+
console.log(alternatingCaps("What did ThEy EAT before?")); // 'what DID they EAT before?'

elementaryAlgo/bleepVowels.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Write a function `bleepVowels` that accepts a string as an argument. The function should return
2+
// a new string where all vowels are replaced with `*`s. Vowels are the letters a, e, i, o, u.
3+
function bleepVowels(str) {
4+
let array = ["a", "e", "i", "o", "u"];
5+
let result = "";
6+
for (let i = 0; i < str.length; i++) {
7+
let char = str[i];
8+
if (array.indexOf(char) > -1) {
9+
result += "*";
10+
} else {
11+
result += char;
12+
}
13+
}
14+
return result;
15+
}
16+
console.log(bleepVowels("skateboard")); // 'sk*t*b**rd'
17+
console.log(bleepVowels("slipper")); // 'sl*pp*r'
18+
console.log(bleepVowels("range")); // 'r*ng*'
19+
console.log(bleepVowels("brisk morning")); // 'br*sk m*rn*ng'

elementaryAlgo/chooseDivisible.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Write a function `chooseDivisibles(numbers, target)` that accepts an array of numbers and a
2+
// target number as arguments. The function should return an array containing elements of the original
3+
// array that are divisible by the target.
4+
const chooseDivisibles = (numbers, target) => {
5+
let result = [];
6+
for (let i = 0; i < numbers.length; i++) {
7+
let num = numbers[i];
8+
if (num % target === 0) {
9+
result.push(num);
10+
}
11+
}
12+
return result;
13+
};
14+
console.log(chooseDivisibles([40, 7, 22, 20, 24], 4)); // [40, 20, 24]
15+
console.log(chooseDivisibles([9, 33, 8, 17], 3)); // [9, 33]
16+
console.log(chooseDivisibles([4, 25, 1000], 10)); // [1000]

elementaryAlgo/commonElement.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Write a function `commonElements` that accepts two arrays as arguments. The function should return
2+
// a new array containing the elements that are found in both of the input arrays. The order of
3+
// the elements in the output array doesn't matter as long as the function returns the correct elements.
4+
const commonElements = (array1, array2) => {
5+
let result = [];
6+
let set1 = new Set(array1);
7+
let set2 = new Set(array2);
8+
for (let elem of set1) {
9+
if (set2.has(elem)) {
10+
result.push(elem);
11+
}
12+
}
13+
return result;
14+
};
15+
let arr1 = ["a", "c", "d", "b"];
16+
let arr2 = ["b", "a", "y"];
17+
console.log(commonElements(arr1, arr2)); // ['a', 'b']
18+
19+
let arr3 = [4, 7];
20+
let arr4 = [32, 7, 1, 4];
21+
console.log(commonElements(arr3, arr4)); // [4, 7]

elementaryAlgo/divisors.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Write a function `divisors` that accepts a number as an argument. The function should return an
2+
// array containing all positive numbers that can divide into the argument.
3+
function divisors(num) {
4+
let result = [];
5+
for (let i = 1; i <= num; i++) {
6+
let eachNum = i;
7+
if (num % eachNum === 0) {
8+
result.push(eachNum);
9+
}
10+
}
11+
return result;
12+
}
13+
console.log(divisors(15)); // [1, 3, 5, 15]
14+
console.log(divisors(7)); // [1, 7]
15+
console.log(divisors(24)); // [1, 2, 3, 4, 6, 8, 12, 24]

elementaryAlgo/filterLongWords.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Write a function `filterLongWords` that accepts an array of strings as an argument. The function
2+
// should return a new array containing only the strings that are less than 5 characters long.
3+
function filterLongWords(array) {
4+
let result = [];
5+
for (let i = 0; i < array.length; i++) {
6+
let word = array[i];
7+
if (word.length < 5) {
8+
result.push(word);
9+
}
10+
}
11+
return result;
12+
}
13+
console.log(filterLongWords(["kale", "cat", "retro", "axe", "heirloom"]));
14+
// ['kale', 'cat', 'axe']
15+
16+
console.log(filterLongWords(["disrupt", "pour", "trade", "pic"]));
17+
// ['pour', 'pic']

elementaryAlgo/lengthiestWord.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Write a function `lengthiestWord` that accepts a sentence string as an argument. The function should
2+
// return the longest word of the sentence. If there is a tie, return the word that appears later
3+
// in the sentence.
4+
const lengthiestWord = (words) => {
5+
let arrayWord = words.split(" ");
6+
let lengthiest = arrayWord[0];
7+
for (let i = 1; i < arrayWord.length; i++) {
8+
let word = arrayWord[i];
9+
if (lengthiest.length <= word.length) {
10+
lengthiest = word;
11+
}
12+
}
13+
return lengthiest;
14+
};
15+
console.log(lengthiestWord("I am pretty hungry")); // 'hungry'
16+
console.log(lengthiestWord("we should think outside of the box")); // 'outside'
17+
console.log(lengthiestWord("down the rabbit hole")); // 'rabbit'
18+
console.log(lengthiestWord("simmer down")); // 'simmer'

elementaryAlgo/makeAcronyms.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Write a function `makeAcronym` that accepts a sentence string as an argument. The function should
2+
// return a string containing the first character of each word in the sentence.
3+
const makeAcronym = (words) => {
4+
let arrayWord = words.split(" ");
5+
let result = "";
6+
for (let i = 0; i < arrayWord.length; i++) {
7+
let word = arrayWord[i][0];
8+
result += word.toUpperCase();
9+
}
10+
return result;
11+
};
12+
console.log(makeAcronym("New York")); // NY
13+
console.log(makeAcronym("same stuff different day")); // SSDD
14+
console.log(makeAcronym("Laugh out loud")); // LOL
15+
console.log(makeAcronym("don't over think stuff")); // DOTS

elementaryAlgo/makeMatrix.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Write a function `makeMatrix(m, n, value)` that accepts three arguments. The function should return
2+
// a 2-dimensional array of height `m` and width `n` that contains the `value` as every element.
3+
const makeMatrix = (m, n, value) => {
4+
let matrix = [];
5+
for (let i = 0; i < m; i++) {
6+
let row = [];
7+
for (let j = 0; j < n; j++) {
8+
row.push(value);
9+
}
10+
matrix.push(row);
11+
}
12+
return matrix;
13+
};
14+
// const makeMatrix = (m, n, value) => {
15+
// let matrix = Array.from(Array(m), () => Array(n).fill(value));
16+
// return matrix
17+
// };
18+
console.log(makeMatrix(3, 5, null));
19+
// [
20+
// [ null, null, null, null, null ],
21+
// [ null, null, null, null, null ],
22+
// [ null, null, null, null, null ]
23+
// ]
24+
25+
console.log(makeMatrix(4, 2, "x"));
26+
// [
27+
// [ 'x', 'x' ],
28+
// [ 'x', 'x' ],
29+
// [ 'x', 'x' ],
30+
// [ 'x', 'x' ]
31+
// ]
32+
33+
console.log(makeMatrix(2, 2, 0));
34+
// [
35+
// [ 0, 0 ],
36+
// [ 0, 0 ]
37+
// ]

elementaryAlgo/maximum.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Write a function `maximum` that accepts an array of numbers as an argument. The function should
2+
// return the largest number of the array. If the array is empty, then the function should return null.
3+
const maximum = (array) => {
4+
if (array.length === 0) return null;
5+
let max = array[0];
6+
for (let i = 1; i < array.length; i++) {
7+
let elem = array[i];
8+
max = Math.max(max, elem);
9+
}
10+
return max;
11+
};
12+
console.log(maximum([5, 6, 3, 7])); // 7
13+
console.log(maximum([17, 15, 19, 11, 2])); // 19
14+
console.log(maximum([])); // null

elementaryAlgo/numOdd.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Write a function `numOdds` that accepts an array of numbers as an argument. The function should
2+
// return a number representing the count of odd elements in the array.
3+
function numOdds(array) {
4+
let count = 0;
5+
for (let i = 0; i < array.length; i++) {
6+
let num = array[i];
7+
if (num % 2 === 1) count++;
8+
}
9+
return count;
10+
}
11+
console.log(numOdds([4, 7, 2, 5, 9])); // 3
12+
console.log(numOdds([11, 31, 58, 99, 21, 60])); // 4
13+
console.log(numOdds([100, 40, 4])); // 0

elementaryAlgo/numberRange.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Write a function `numberRange(min, max, step)` that accepts three numbers as arguments, `min`,
2+
// `max`, and `step`. The function should return all numbers between `min` and `max` at `step` intervals.
3+
// `min` and `max` are inclusive.
4+
const numberRange = (min, max, step) => {
5+
let result = [];
6+
for (let i = min; i <= max; i += step) {
7+
result.push(i);
8+
}
9+
return result;
10+
};
11+
console.log(numberRange(10, 40, 5)); // [10, 15, 20, 25, 30, 35, 40]
12+
console.log(numberRange(14, 24, 3)); // [14, 17, 20, 23]
13+
console.log(numberRange(8, 35, 6)); // [8, 14, 20, 26, 32]

elementaryAlgo/pairPrint.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Write a function `pairPrint` that accepts an array as an argument. The function should print
2+
// all unique pairs of elements in the array. The function doesn't need to return any value. It
3+
// should just print to the terminal.
4+
function pairPrint(arr) {
5+
for (let i = 0; i < arr.length; i++) {
6+
for (let j = i + 1; j < arr.length; j++) {
7+
console.log(arr[i], arr[j]);
8+
}
9+
}
10+
}
11+
12+
13+
pairPrint(["artichoke", "broccoli", "carrot", "daikon"]);
14+
// prints
15+
// artichoke - broccoli
16+
// artichoke - carrot
17+
// artichoke - daikon
18+
// broccoli - carrot
19+
// broccoli - daikon
20+
// carrot - daikon
21+
22+
//pairPrint(["apple", "banana", "clementine"]);
23+
// prints
24+
// apple - banana
25+
// apple - clementine
26+
// banana - clementine

elementaryAlgo/print2d.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Write a function `print2d` that accepts a two-dimensional array as an argument. The function
2+
// should print all inner elements of the array.
3+
const print2d = (array) => {
4+
for (let i = 0; i < array.length; i++) {
5+
let innerElem = array[i];
6+
for (let j = 0; j < innerElem.length; j++) {
7+
console.log(innerElem[j]);
8+
}
9+
}
10+
};
11+
let array1 = [
12+
["a", "b", "c", "d"],
13+
["e", "f"],
14+
["g", "h", "i"],
15+
];
16+
print2d(array1);
17+
// prints
18+
// a
19+
// b
20+
// c
21+
// d
22+
// e
23+
// f
24+
// g
25+
// h
26+
// i
27+
28+
let array2 = [[9, 3, 4], [11], [42, 100]];
29+
print2d(array2);
30+
// prints
31+
// 9
32+
// 3
33+
// 4
34+
// 11
35+
// 42
36+
// 100

elementaryAlgo/printCombination.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Write a function `printCombinations`that accepts two arrays as arguments. The function should
2+
// print all combinations of the elements generated by taking an element from the first array and
3+
// and an element from the second array. The function doesn't need to return any value. It
4+
// should just print to the terminal.
5+
function printCombinations(array1, array2) {
6+
for (let i = 0; i < array1.length; i++) {
7+
const element1 = array1[i];
8+
for (let j = 0; j < array2.length; j++) {
9+
let element2 = array2[j];
10+
console.log(element1, element2);
11+
}
12+
}
13+
}
14+
15+
let colors = ["gray", "cream", "cyan"];
16+
let clothes = ["shirt", "flannel"];
17+
18+
printCombinations(colors, clothes);
19+
// prints
20+
// gray shirt
21+
// gray flannel
22+
// cream shirt
23+
// cream flannel
24+
// cyan shirt
25+
// cyan flannel
26+
27+
//printCombinations(["hot", "cold"], ["soup", "tea"]);
28+
// prints
29+
// hot soup
30+
// hot tea
31+
// cold soup
32+
// cold tea

elementaryAlgo/recursionArray.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//Given an array of integers, write a function that return the sum all of it element using recursion
2+
const sum = (array) => {
3+
if (array.length === 0) return 0;
4+
let rest = array.slice(1);
5+
return array[0] + sum(rest);
6+
};
7+
console.log(sum([2, 5, 6, 8, 9]));
8+
//Time: O(n^2)
9+
//Space: O(n)
10+
// can improve our space complexity like this
11+
const sumImprove = (array) => {
12+
return _sum(array, 0);
13+
};
14+
const _sum = (array, idx) => {
15+
if (array.length === idx) return 0;
16+
return array[idx] + _sum(array, idx + 1);
17+
};
18+
console.log(sumImprove([2, 5, 6, 8, 9]));
19+
//Time: O(n)
20+
//Space: O(n)

elementaryAlgo/removeFirstVowel.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Write a function `removeFirstVowel` that accepts a string as an argument. The function should return
2+
// the string with it's first vowel removed.
3+
const removeFirstVowel = (words) => {
4+
let vowels = ["a", "e", "i", "o", "u"];
5+
for (let i = 0; i < words.length; i++) {
6+
let word = words[i];
7+
if (vowels.includes(word)) {
8+
let copy = words.split("");
9+
copy.splice(i, 1);
10+
return copy.join("");
11+
}
12+
}
13+
return words;
14+
};
15+
console.log(removeFirstVowel("volcano")); // 'vlcano'
16+
console.log(removeFirstVowel("celery")); // 'clery'
17+
console.log(removeFirstVowel("juice")); // 'jice'
18+
console.log(removeFirstVowel("ghshsh"));

elementaryAlgo/removeShortWords.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Write a function `removeShortWords` that accepts a sentence string as an argument. The function
2+
// should return a new sentence where all of the words shorter than 4 characters are removed.
3+
const removeShortWords = (words) => {
4+
let result = "";
5+
let arrayWords = words.split(" ");
6+
for (let i = 0; i < arrayWords.length; i++) {
7+
let word = arrayWords[i];
8+
if (word.length < 4) continue;
9+
else result += word + " ";
10+
}
11+
return result;
12+
};
13+
console.log(removeShortWords("knock on the door will you")); // 'knock door will'
14+
console.log(removeShortWords("a terrible plan")); // 'terrible plan'
15+
console.log(removeShortWords("run faster that way")); // 'faster that'

elementaryAlgo/reverseArray.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Write a function `reverseArray` that accepts an array as an argument. The function should return a
2+
// array containing the elements of the original array in reverse order.
3+
const reverseArray = (array) => {
4+
let result = [];
5+
for (let i = array.length - 1; i >= 0; i--) {
6+
let element = array[i];
7+
result.push(element);
8+
}
9+
return result;
10+
};
11+
console.log(reverseArray(["zero", "one", "two", "three"])); // ['three', 'two', 'one', 'zero']
12+
console.log(reverseArray([7, 1, 8])); // [8, 1, 7]

0 commit comments

Comments
 (0)