From a9beefb6432af648750b281549174f5fe10be954 Mon Sep 17 00:00:00 2001 From: rabobahago Date: Sun, 8 Jan 2023 02:06:29 +0100 Subject: [PATCH] elementary algorithm JavaScript solutions added --- elementaryAlgo/alternateCap.js | 17 +++++++++++++ elementaryAlgo/bleepVowels.js | 19 ++++++++++++++ elementaryAlgo/chooseDivisible.js | 16 ++++++++++++ elementaryAlgo/commonElement.js | 21 +++++++++++++++ elementaryAlgo/divisors.js | 15 +++++++++++ elementaryAlgo/filterLongWords.js | 17 +++++++++++++ elementaryAlgo/lengthiestWord.js | 18 +++++++++++++ elementaryAlgo/makeAcronyms.js | 15 +++++++++++ elementaryAlgo/makeMatrix.js | 37 +++++++++++++++++++++++++++ elementaryAlgo/maximum.js | 14 ++++++++++ elementaryAlgo/numOdd.js | 13 ++++++++++ elementaryAlgo/numberRange.js | 13 ++++++++++ elementaryAlgo/pairPrint.js | 26 +++++++++++++++++++ elementaryAlgo/print2d.js | 36 ++++++++++++++++++++++++++ elementaryAlgo/printCombination.js | 32 +++++++++++++++++++++++ elementaryAlgo/recursionArray.js | 20 +++++++++++++++ elementaryAlgo/removeFirstVowel.js | 18 +++++++++++++ elementaryAlgo/removeShortWords.js | 15 +++++++++++ elementaryAlgo/reverseArray.js | 12 +++++++++ elementaryAlgo/stayPositive.js | 15 +++++++++++ elementaryAlgo/stringToLength.js | 15 +++++++++++ elementaryAlgo/total.js | 22 ++++++++++++++++ elementaryAlgo/totalProducts.js | 24 +++++++++++++++++ elementaryAlgo/twoSum.js | 15 +++++++++++ elementaryAlgo/twoSumPairs.js | 18 +++++++++++++ elementaryAlgo/wordCount.js | 15 +++++++++++ elementaryAlgo/yourAverageFunc.js | 16 ++++++++++++ elementaryAlgo/zipper.js | 41 ++++++++++++++++++++++++++++++ 28 files changed, 555 insertions(+) create mode 100644 elementaryAlgo/alternateCap.js create mode 100644 elementaryAlgo/bleepVowels.js create mode 100644 elementaryAlgo/chooseDivisible.js create mode 100644 elementaryAlgo/commonElement.js create mode 100644 elementaryAlgo/divisors.js create mode 100644 elementaryAlgo/filterLongWords.js create mode 100644 elementaryAlgo/lengthiestWord.js create mode 100644 elementaryAlgo/makeAcronyms.js create mode 100644 elementaryAlgo/makeMatrix.js create mode 100644 elementaryAlgo/maximum.js create mode 100644 elementaryAlgo/numOdd.js create mode 100644 elementaryAlgo/numberRange.js create mode 100644 elementaryAlgo/pairPrint.js create mode 100644 elementaryAlgo/print2d.js create mode 100644 elementaryAlgo/printCombination.js create mode 100644 elementaryAlgo/recursionArray.js create mode 100644 elementaryAlgo/removeFirstVowel.js create mode 100644 elementaryAlgo/removeShortWords.js create mode 100644 elementaryAlgo/reverseArray.js create mode 100644 elementaryAlgo/stayPositive.js create mode 100644 elementaryAlgo/stringToLength.js create mode 100644 elementaryAlgo/total.js create mode 100644 elementaryAlgo/totalProducts.js create mode 100644 elementaryAlgo/twoSum.js create mode 100644 elementaryAlgo/twoSumPairs.js create mode 100644 elementaryAlgo/wordCount.js create mode 100644 elementaryAlgo/yourAverageFunc.js create mode 100644 elementaryAlgo/zipper.js diff --git a/elementaryAlgo/alternateCap.js b/elementaryAlgo/alternateCap.js new file mode 100644 index 0000000..a9bf474 --- /dev/null +++ b/elementaryAlgo/alternateCap.js @@ -0,0 +1,17 @@ +// Write a function `alternatingCaps` that accepts a sentence string as an argument. The function should +// return the sentence where words alternate between lowercase and uppercase. +const alternatingCaps = (words) => { + let result = ""; + let arraySplit = words.split(" "); + for (let i = 0; i < arraySplit.length; i++) { + let word = arraySplit[i]; + if (i % 2 === 0) { + result += word.toLowerCase() + " "; + } else { + result += word.toUpperCase() + " "; + } + } + return result; +}; +console.log(alternatingCaps("take them to school")); // 'take THEM to SCHOOL' +console.log(alternatingCaps("What did ThEy EAT before?")); // 'what DID they EAT before?' diff --git a/elementaryAlgo/bleepVowels.js b/elementaryAlgo/bleepVowels.js new file mode 100644 index 0000000..e2c618d --- /dev/null +++ b/elementaryAlgo/bleepVowels.js @@ -0,0 +1,19 @@ +// Write a function `bleepVowels` that accepts a string as an argument. The function should return +// a new string where all vowels are replaced with `*`s. Vowels are the letters a, e, i, o, u. +function bleepVowels(str) { + let array = ["a", "e", "i", "o", "u"]; + let result = ""; + for (let i = 0; i < str.length; i++) { + let char = str[i]; + if (array.indexOf(char) > -1) { + result += "*"; + } else { + result += char; + } + } + return result; +} +console.log(bleepVowels("skateboard")); // 'sk*t*b**rd' +console.log(bleepVowels("slipper")); // 'sl*pp*r' +console.log(bleepVowels("range")); // 'r*ng*' +console.log(bleepVowels("brisk morning")); // 'br*sk m*rn*ng' diff --git a/elementaryAlgo/chooseDivisible.js b/elementaryAlgo/chooseDivisible.js new file mode 100644 index 0000000..fc5b1d5 --- /dev/null +++ b/elementaryAlgo/chooseDivisible.js @@ -0,0 +1,16 @@ +// Write a function `chooseDivisibles(numbers, target)` that accepts an array of numbers and a +// target number as arguments. The function should return an array containing elements of the original +// array that are divisible by the target. +const chooseDivisibles = (numbers, target) => { + let result = []; + for (let i = 0; i < numbers.length; i++) { + let num = numbers[i]; + if (num % target === 0) { + result.push(num); + } + } + return result; +}; +console.log(chooseDivisibles([40, 7, 22, 20, 24], 4)); // [40, 20, 24] +console.log(chooseDivisibles([9, 33, 8, 17], 3)); // [9, 33] +console.log(chooseDivisibles([4, 25, 1000], 10)); // [1000] diff --git a/elementaryAlgo/commonElement.js b/elementaryAlgo/commonElement.js new file mode 100644 index 0000000..9388309 --- /dev/null +++ b/elementaryAlgo/commonElement.js @@ -0,0 +1,21 @@ +// Write a function `commonElements` that accepts two arrays as arguments. The function should return +// a new array containing the elements that are found in both of the input arrays. The order of +// the elements in the output array doesn't matter as long as the function returns the correct elements. +const commonElements = (array1, array2) => { + let result = []; + let set1 = new Set(array1); + let set2 = new Set(array2); + for (let elem of set1) { + if (set2.has(elem)) { + result.push(elem); + } + } + return result; +}; +let arr1 = ["a", "c", "d", "b"]; +let arr2 = ["b", "a", "y"]; +console.log(commonElements(arr1, arr2)); // ['a', 'b'] + +let arr3 = [4, 7]; +let arr4 = [32, 7, 1, 4]; +console.log(commonElements(arr3, arr4)); // [4, 7] diff --git a/elementaryAlgo/divisors.js b/elementaryAlgo/divisors.js new file mode 100644 index 0000000..7ffe447 --- /dev/null +++ b/elementaryAlgo/divisors.js @@ -0,0 +1,15 @@ +// Write a function `divisors` that accepts a number as an argument. The function should return an +// array containing all positive numbers that can divide into the argument. +function divisors(num) { + let result = []; + for (let i = 1; i <= num; i++) { + let eachNum = i; + if (num % eachNum === 0) { + result.push(eachNum); + } + } + return result; +} +console.log(divisors(15)); // [1, 3, 5, 15] +console.log(divisors(7)); // [1, 7] +console.log(divisors(24)); // [1, 2, 3, 4, 6, 8, 12, 24] diff --git a/elementaryAlgo/filterLongWords.js b/elementaryAlgo/filterLongWords.js new file mode 100644 index 0000000..99b097a --- /dev/null +++ b/elementaryAlgo/filterLongWords.js @@ -0,0 +1,17 @@ +// Write a function `filterLongWords` that accepts an array of strings as an argument. The function +// should return a new array containing only the strings that are less than 5 characters long. +function filterLongWords(array) { + let result = []; + for (let i = 0; i < array.length; i++) { + let word = array[i]; + if (word.length < 5) { + result.push(word); + } + } + return result; +} +console.log(filterLongWords(["kale", "cat", "retro", "axe", "heirloom"])); +// ['kale', 'cat', 'axe'] + +console.log(filterLongWords(["disrupt", "pour", "trade", "pic"])); +// ['pour', 'pic'] diff --git a/elementaryAlgo/lengthiestWord.js b/elementaryAlgo/lengthiestWord.js new file mode 100644 index 0000000..af76bfc --- /dev/null +++ b/elementaryAlgo/lengthiestWord.js @@ -0,0 +1,18 @@ +// Write a function `lengthiestWord` that accepts a sentence string as an argument. The function should +// return the longest word of the sentence. If there is a tie, return the word that appears later +// in the sentence. +const lengthiestWord = (words) => { + let arrayWord = words.split(" "); + let lengthiest = arrayWord[0]; + for (let i = 1; i < arrayWord.length; i++) { + let word = arrayWord[i]; + if (lengthiest.length <= word.length) { + lengthiest = word; + } + } + return lengthiest; +}; +console.log(lengthiestWord("I am pretty hungry")); // 'hungry' +console.log(lengthiestWord("we should think outside of the box")); // 'outside' +console.log(lengthiestWord("down the rabbit hole")); // 'rabbit' +console.log(lengthiestWord("simmer down")); // 'simmer' diff --git a/elementaryAlgo/makeAcronyms.js b/elementaryAlgo/makeAcronyms.js new file mode 100644 index 0000000..2915a74 --- /dev/null +++ b/elementaryAlgo/makeAcronyms.js @@ -0,0 +1,15 @@ +// Write a function `makeAcronym` that accepts a sentence string as an argument. The function should +// return a string containing the first character of each word in the sentence. +const makeAcronym = (words) => { + let arrayWord = words.split(" "); + let result = ""; + for (let i = 0; i < arrayWord.length; i++) { + let word = arrayWord[i][0]; + result += word.toUpperCase(); + } + return result; +}; +console.log(makeAcronym("New York")); // NY +console.log(makeAcronym("same stuff different day")); // SSDD +console.log(makeAcronym("Laugh out loud")); // LOL +console.log(makeAcronym("don't over think stuff")); // DOTS diff --git a/elementaryAlgo/makeMatrix.js b/elementaryAlgo/makeMatrix.js new file mode 100644 index 0000000..195d208 --- /dev/null +++ b/elementaryAlgo/makeMatrix.js @@ -0,0 +1,37 @@ +// Write a function `makeMatrix(m, n, value)` that accepts three arguments. The function should return +// a 2-dimensional array of height `m` and width `n` that contains the `value` as every element. +const makeMatrix = (m, n, value) => { + let matrix = []; + for (let i = 0; i < m; i++) { + let row = []; + for (let j = 0; j < n; j++) { + row.push(value); + } + matrix.push(row); + } + return matrix; +}; +// const makeMatrix = (m, n, value) => { +// let matrix = Array.from(Array(m), () => Array(n).fill(value)); +// return matrix +// }; +console.log(makeMatrix(3, 5, null)); +// [ +// [ null, null, null, null, null ], +// [ null, null, null, null, null ], +// [ null, null, null, null, null ] +// ] + +console.log(makeMatrix(4, 2, "x")); +// [ +// [ 'x', 'x' ], +// [ 'x', 'x' ], +// [ 'x', 'x' ], +// [ 'x', 'x' ] +// ] + +console.log(makeMatrix(2, 2, 0)); +// [ +// [ 0, 0 ], +// [ 0, 0 ] +// ] diff --git a/elementaryAlgo/maximum.js b/elementaryAlgo/maximum.js new file mode 100644 index 0000000..e7052fd --- /dev/null +++ b/elementaryAlgo/maximum.js @@ -0,0 +1,14 @@ +// Write a function `maximum` that accepts an array of numbers as an argument. The function should +// return the largest number of the array. If the array is empty, then the function should return null. +const maximum = (array) => { + if (array.length === 0) return null; + let max = array[0]; + for (let i = 1; i < array.length; i++) { + let elem = array[i]; + max = Math.max(max, elem); + } + return max; +}; +console.log(maximum([5, 6, 3, 7])); // 7 +console.log(maximum([17, 15, 19, 11, 2])); // 19 +console.log(maximum([])); // null diff --git a/elementaryAlgo/numOdd.js b/elementaryAlgo/numOdd.js new file mode 100644 index 0000000..2b8f9df --- /dev/null +++ b/elementaryAlgo/numOdd.js @@ -0,0 +1,13 @@ +// Write a function `numOdds` that accepts an array of numbers as an argument. The function should +// return a number representing the count of odd elements in the array. +function numOdds(array) { + let count = 0; + for (let i = 0; i < array.length; i++) { + let num = array[i]; + if (num % 2 === 1) count++; + } + return count; +} +console.log(numOdds([4, 7, 2, 5, 9])); // 3 +console.log(numOdds([11, 31, 58, 99, 21, 60])); // 4 +console.log(numOdds([100, 40, 4])); // 0 diff --git a/elementaryAlgo/numberRange.js b/elementaryAlgo/numberRange.js new file mode 100644 index 0000000..f7850a3 --- /dev/null +++ b/elementaryAlgo/numberRange.js @@ -0,0 +1,13 @@ +// Write a function `numberRange(min, max, step)` that accepts three numbers as arguments, `min`, +// `max`, and `step`. The function should return all numbers between `min` and `max` at `step` intervals. +// `min` and `max` are inclusive. +const numberRange = (min, max, step) => { + let result = []; + for (let i = min; i <= max; i += step) { + result.push(i); + } + return result; +}; +console.log(numberRange(10, 40, 5)); // [10, 15, 20, 25, 30, 35, 40] +console.log(numberRange(14, 24, 3)); // [14, 17, 20, 23] +console.log(numberRange(8, 35, 6)); // [8, 14, 20, 26, 32] diff --git a/elementaryAlgo/pairPrint.js b/elementaryAlgo/pairPrint.js new file mode 100644 index 0000000..fda28dd --- /dev/null +++ b/elementaryAlgo/pairPrint.js @@ -0,0 +1,26 @@ +// Write a function `pairPrint` that accepts an array as an argument. The function should print +// all unique pairs of elements in the array. The function doesn't need to return any value. It +// should just print to the terminal. +function pairPrint(arr) { + for (let i = 0; i < arr.length; i++) { + for (let j = i + 1; j < arr.length; j++) { + console.log(arr[i], arr[j]); + } + } +} + + +pairPrint(["artichoke", "broccoli", "carrot", "daikon"]); +// prints +// artichoke - broccoli +// artichoke - carrot +// artichoke - daikon +// broccoli - carrot +// broccoli - daikon +// carrot - daikon + +//pairPrint(["apple", "banana", "clementine"]); +// prints +// apple - banana +// apple - clementine +// banana - clementine diff --git a/elementaryAlgo/print2d.js b/elementaryAlgo/print2d.js new file mode 100644 index 0000000..43e2492 --- /dev/null +++ b/elementaryAlgo/print2d.js @@ -0,0 +1,36 @@ +// Write a function `print2d` that accepts a two-dimensional array as an argument. The function +// should print all inner elements of the array. +const print2d = (array) => { + for (let i = 0; i < array.length; i++) { + let innerElem = array[i]; + for (let j = 0; j < innerElem.length; j++) { + console.log(innerElem[j]); + } + } +}; +let array1 = [ + ["a", "b", "c", "d"], + ["e", "f"], + ["g", "h", "i"], +]; +print2d(array1); +// prints +// a +// b +// c +// d +// e +// f +// g +// h +// i + +let array2 = [[9, 3, 4], [11], [42, 100]]; +print2d(array2); +// prints +// 9 +// 3 +// 4 +// 11 +// 42 +// 100 diff --git a/elementaryAlgo/printCombination.js b/elementaryAlgo/printCombination.js new file mode 100644 index 0000000..32505de --- /dev/null +++ b/elementaryAlgo/printCombination.js @@ -0,0 +1,32 @@ +// Write a function `printCombinations`that accepts two arrays as arguments. The function should +// print all combinations of the elements generated by taking an element from the first array and +// and an element from the second array. The function doesn't need to return any value. It +// should just print to the terminal. +function printCombinations(array1, array2) { + for (let i = 0; i < array1.length; i++) { + const element1 = array1[i]; + for (let j = 0; j < array2.length; j++) { + let element2 = array2[j]; + console.log(element1, element2); + } + } +} + +let colors = ["gray", "cream", "cyan"]; +let clothes = ["shirt", "flannel"]; + +printCombinations(colors, clothes); +// prints +// gray shirt +// gray flannel +// cream shirt +// cream flannel +// cyan shirt +// cyan flannel + +//printCombinations(["hot", "cold"], ["soup", "tea"]); +// prints +// hot soup +// hot tea +// cold soup +// cold tea diff --git a/elementaryAlgo/recursionArray.js b/elementaryAlgo/recursionArray.js new file mode 100644 index 0000000..26273b9 --- /dev/null +++ b/elementaryAlgo/recursionArray.js @@ -0,0 +1,20 @@ +//Given an array of integers, write a function that return the sum all of it element using recursion +const sum = (array) => { + if (array.length === 0) return 0; + let rest = array.slice(1); + return array[0] + sum(rest); +}; +console.log(sum([2, 5, 6, 8, 9])); +//Time: O(n^2) +//Space: O(n) +// can improve our space complexity like this +const sumImprove = (array) => { + return _sum(array, 0); +}; +const _sum = (array, idx) => { + if (array.length === idx) return 0; + return array[idx] + _sum(array, idx + 1); +}; +console.log(sumImprove([2, 5, 6, 8, 9])); +//Time: O(n) +//Space: O(n) diff --git a/elementaryAlgo/removeFirstVowel.js b/elementaryAlgo/removeFirstVowel.js new file mode 100644 index 0000000..e10a3fc --- /dev/null +++ b/elementaryAlgo/removeFirstVowel.js @@ -0,0 +1,18 @@ +// Write a function `removeFirstVowel` that accepts a string as an argument. The function should return +// the string with it's first vowel removed. +const removeFirstVowel = (words) => { + let vowels = ["a", "e", "i", "o", "u"]; + for (let i = 0; i < words.length; i++) { + let word = words[i]; + if (vowels.includes(word)) { + let copy = words.split(""); + copy.splice(i, 1); + return copy.join(""); + } + } + return words; +}; +console.log(removeFirstVowel("volcano")); // 'vlcano' +console.log(removeFirstVowel("celery")); // 'clery' +console.log(removeFirstVowel("juice")); // 'jice' +console.log(removeFirstVowel("ghshsh")); diff --git a/elementaryAlgo/removeShortWords.js b/elementaryAlgo/removeShortWords.js new file mode 100644 index 0000000..1218a0d --- /dev/null +++ b/elementaryAlgo/removeShortWords.js @@ -0,0 +1,15 @@ +// Write a function `removeShortWords` that accepts a sentence string as an argument. The function +// should return a new sentence where all of the words shorter than 4 characters are removed. +const removeShortWords = (words) => { + let result = ""; + let arrayWords = words.split(" "); + for (let i = 0; i < arrayWords.length; i++) { + let word = arrayWords[i]; + if (word.length < 4) continue; + else result += word + " "; + } + return result; +}; +console.log(removeShortWords("knock on the door will you")); // 'knock door will' +console.log(removeShortWords("a terrible plan")); // 'terrible plan' +console.log(removeShortWords("run faster that way")); // 'faster that' diff --git a/elementaryAlgo/reverseArray.js b/elementaryAlgo/reverseArray.js new file mode 100644 index 0000000..9cb3311 --- /dev/null +++ b/elementaryAlgo/reverseArray.js @@ -0,0 +1,12 @@ +// Write a function `reverseArray` that accepts an array as an argument. The function should return a +// array containing the elements of the original array in reverse order. +const reverseArray = (array) => { + let result = []; + for (let i = array.length - 1; i >= 0; i--) { + let element = array[i]; + result.push(element); + } + return result; +}; +console.log(reverseArray(["zero", "one", "two", "three"])); // ['three', 'two', 'one', 'zero'] +console.log(reverseArray([7, 1, 8])); // [8, 1, 7] diff --git a/elementaryAlgo/stayPositive.js b/elementaryAlgo/stayPositive.js new file mode 100644 index 0000000..e3411c6 --- /dev/null +++ b/elementaryAlgo/stayPositive.js @@ -0,0 +1,15 @@ +// Write a function `stayPositive` that accepts an array of numbers as an argument. The function should +// return an array containing only the positive numbers. +function stayPositive(array) { + let result = []; + for (let i = 0; i < array.length; i++) { + let num = array[i]; + if (num > 0) { + result.push(num); + } + } + return result; +} +console.log(stayPositive([10, -4, 3, 6])); // [10, 3, 6] +console.log(stayPositive([-5, 11, -40, 30.3, -2])); // [11, 30.3] +console.log(stayPositive([-11, -30])); // [] diff --git a/elementaryAlgo/stringToLength.js b/elementaryAlgo/stringToLength.js new file mode 100644 index 0000000..70cf054 --- /dev/null +++ b/elementaryAlgo/stringToLength.js @@ -0,0 +1,15 @@ +// Write a function `stringsToLengths` that accepts an array of strings as an argument. The function +// should return a new array containing the lengths of the elements of the original array. +function stringsToLengths(array) { + let result = []; + for (let i = 0; i < array.length; i++) { + let word = array[i]; + result.push(word.length); + } + return result; +} +console.log(stringsToLengths(["belly", "echo", "irony", "pickled"])); +// [5, 4, 5, 7] + +console.log(stringsToLengths(["on", "off", "handmade"])); +// [2, 3, 8] diff --git a/elementaryAlgo/total.js b/elementaryAlgo/total.js new file mode 100644 index 0000000..1030568 --- /dev/null +++ b/elementaryAlgo/total.js @@ -0,0 +1,22 @@ +// Write a function `total` that accepts an array of numbers as an argument. The function should return +// the total sum of all elements of the array. + +function total(array) { + let sum = 0; + for (let i = 0; i < array.length; i++) { + sum += array[i]; + } + return sum; +} +console.log(total([3, 2, 8])); // 13 +console.log(total([-5, 7, 4, 6])); // 12 +console.log(total([7])); // 7 +console.log(total([])); // 0 + +let obj = { + name: "right", + number: 3, +}; + +if (key in obj) { +} diff --git a/elementaryAlgo/totalProducts.js b/elementaryAlgo/totalProducts.js new file mode 100644 index 0000000..d9f25eb --- /dev/null +++ b/elementaryAlgo/totalProducts.js @@ -0,0 +1,24 @@ +// Write a function `totalProduct(array)` that accepts a 2D array of numbers. The function should return +// the total product of all numbers in the array. +const totalProduct = (array) => { + let totalProduct = 1; + for (let i = 0; i < array.length; i++) { + let product = array[i]; + for (let j = 0; j < product.length; j++) { + totalProduct *= product[j]; + } + } + return totalProduct; +}; +let array1 = [ + [3, 5, 2], + [6, 2], +]; +console.log(totalProduct(array1)); // 360 + +let array2 = [ + [4, 6], + [2, 3], + [1, 2], +]; +console.log(totalProduct(array2)); // 288 diff --git a/elementaryAlgo/twoSum.js b/elementaryAlgo/twoSum.js new file mode 100644 index 0000000..c8dc9e8 --- /dev/null +++ b/elementaryAlgo/twoSum.js @@ -0,0 +1,15 @@ +// Write a function `twoSum(numbers, target)` that accepts an array of numbers and a target number +// as an argument. The function should return a boolean indicating whether or not there exists a pair +// of distinct elements in the array that sum to the target. +function twoSum(numbers, target) { + for (let i = 0; i < numbers.length; i++) { + for (let j = i + 1; j < numbers.length; j++) { + if (numbers[i] + numbers[j] === target) return true; + } + } + return false; +} +console.log(twoSum([2, 3, 5, 9], 7)); // true +console.log(twoSum([2, 3, 5, 9], 4)); // false +console.log(twoSum([6, 3, 4], 10)); // true +console.log(twoSum([6, 5, 1], 10)); // false diff --git a/elementaryAlgo/twoSumPairs.js b/elementaryAlgo/twoSumPairs.js new file mode 100644 index 0000000..653a20e --- /dev/null +++ b/elementaryAlgo/twoSumPairs.js @@ -0,0 +1,18 @@ +// Write a function `twoSumPairs(numbers, target)` that accepts an array of numbers and a target number +// as arguments. The function should return a 2D array containing all unique pairs of elements that +// sum to the target. +const twoSumPairs = (numbers, target) => { + let result = []; + for (let i = 0; i < numbers.length; i++) { + for (let j = i + 1; j < numbers.length; j++) { + if (numbers[i] + numbers[j] === target) { + result.push([numbers[i], numbers[j]]); + } + } + } + return result; +}; +console.log(twoSumPairs([2, 3, 4, 6, 5], 8)); // [ [2, 6], [3, 5] ] +console.log(twoSumPairs([10, 7, 4, 5, 2], 12)); // [ [10, 2], [7, 5] ] +console.log(twoSumPairs([3, 9, 8], 11)); // [ [3, 8] ] +console.log(twoSumPairs([3, 9, 8], 10)); // [ ] diff --git a/elementaryAlgo/wordCount.js b/elementaryAlgo/wordCount.js new file mode 100644 index 0000000..244fd05 --- /dev/null +++ b/elementaryAlgo/wordCount.js @@ -0,0 +1,15 @@ +// Write a function `wordCount(sentence, targetWords)` that accepts a sentence string and an array of +// `targetWords`. The function should return a count of the number of words of the sentence that are +// in `targetWords`. +const wordCount = (sentence, targetWords) => { + let count = 0; + for (let i = 0; i < targetWords.length; i++) { + if (sentence.indexOf(targetWords[i]) > -1) { + count += 1; + } + } + return count; +}; +console.log(wordCount("open the window please", ["please", "open", "sorry"])); // 2 +console.log(wordCount("drive to the cinema", ["the", "driver"])); // 1 +console.log(wordCount("can I have that can", ["can", "I"])); // 3 diff --git a/elementaryAlgo/yourAverageFunc.js b/elementaryAlgo/yourAverageFunc.js new file mode 100644 index 0000000..cc4e6e3 --- /dev/null +++ b/elementaryAlgo/yourAverageFunc.js @@ -0,0 +1,16 @@ +// Write a function `yourAverageFunction` that accepts an array of numbers as an argument. The +// function should return the average of all elements of the array. If the input array is empty, +// then the function should return null. +const yourAverageFunction = (array) => { + if (array.length === 0) return null; + let sum = 0; + for (let i = 0; i < array.length; i++) { + let num = array[i]; + sum += num; + } + return sum / array.length; +}; +console.log(yourAverageFunction([5, 2, 7, 24])); // 9.5 +console.log(yourAverageFunction([100, 6])); // 53 +console.log(yourAverageFunction([31, 32, 40, 12, 33])); // 29.6 +console.log(yourAverageFunction([])); // null diff --git a/elementaryAlgo/zipper.js b/elementaryAlgo/zipper.js new file mode 100644 index 0000000..3537a23 --- /dev/null +++ b/elementaryAlgo/zipper.js @@ -0,0 +1,41 @@ +// Write a function `zipper` that accepts two arrays as arguments. The function should return a 2D +// array containing pairs of elements at the same indices. You can assume that the arrays have the +// same length. +const zipper = (array1, array2) => { + let result = []; + for (let i = 0; i < array1.length; i++) { + let elem1 = array1[i]; + let elem2 = array2[i]; + result.push([elem1, elem2]); + } + return result; +}; +// const zipper = (array1, array2) => { +// let result = [] +// for (let i = 0; i < array1.length; i++) { +// for (let j = 0; j < array2.length; j++) { +// if(i === j){ +// result.push([array1[i], array2[j]]) +// } +// } +// } +// return result +// }; +let array1 = ["a", "b", "c", "d"]; +let array2 = [-1, -2, -3, -4]; +console.log(zipper(array1, array2)); +// [ +// ['a', -1], +// ['b', -2], +// ['c', -3], +// ['d', -4], +// ] + +let array3 = ["whisper", "talk", "shout"]; +let array4 = ["quiet", "normal", "loud"]; +console.log(zipper(array3, array4)); +// [ +// ['whisper', 'quiet'], +// ['talk', 'normal'], +// ['shout', 'loud'], +// ]