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

0 commit comments

Comments
 (0)