diff --git a/java/0205_isomorphic_strings b/java/0205_isomorphic_strings.java similarity index 100% rename from java/0205_isomorphic_strings rename to java/0205_isomorphic_strings.java diff --git a/javascript/1941-check-if-all-characters-have-equal-number-of-occurrences.js b/javascript/1941-check-if-all-characters-have-equal-number-of-occurrences.js new file mode 100644 index 000000000..320ef89ea --- /dev/null +++ b/javascript/1941-check-if-all-characters-have-equal-number-of-occurrences.js @@ -0,0 +1,23 @@ +/** + * @param {string} s + * @return {boolean} + */ +var areOccurrencesEqual = function (s) { + let arr = [] + for (i = 0; i < s.length; i++) { + let count = 0; + for (j = 0; j < s.length; j++) { + if (s[i] == s[j]) { + if (i > j) { + break; + } + count++ + } + } + if (count != 0) { + arr.push(count) + } + } + let set = new Set(arr) + return set.size == 1 +}; \ No newline at end of file diff --git a/javascript/2626-array-reduce-transformation.js b/javascript/2626-array-reduce-transformation.js new file mode 100644 index 000000000..1bb5b9d92 --- /dev/null +++ b/javascript/2626-array-reduce-transformation.js @@ -0,0 +1,22 @@ +/** + * @param {number[]} nums + * @param {Function} fn + * @param {number} init + * @return {number} + */ +var reduce = function (nums, fn, init) { + var val = init; + for (let i = 0; i < nums.length; i++) { + val = fn(val, nums[i]) + } + return val; +}; + + +var reduce = function (nums, fn, init) { + + for (let i = 0; i < nums.length; i++) { + init = fn(init, nums[i]) + } + return init +}; \ No newline at end of file diff --git a/javascript/2629-function-composition.js b/javascript/2629-function-composition.js new file mode 100644 index 000000000..2e1a679f2 --- /dev/null +++ b/javascript/2629-function-composition.js @@ -0,0 +1,22 @@ +/** + * @param {Function[]} functions + * @return {Function} + */ +var compose = function (functions) { + return function (x) { + if (functions.length === 0) { + return x; + } + var result = x; + for (var i = functions.length - 1; i >= 0; i--) { + result = functions[i](result); + } + + return result; + } +}; + +/** + * const fn = compose([x => x + 1, x => 2 * x]) + * fn(4) // 9 + */ \ No newline at end of file diff --git a/javascript/2634-filter-elements-from-array.js b/javascript/2634-filter-elements-from-array.js new file mode 100644 index 000000000..8d9d3de5d --- /dev/null +++ b/javascript/2634-filter-elements-from-array.js @@ -0,0 +1,9 @@ +/** + * @param {number[]} arr + * @param {Function} fn + * @return {number[]} + */ +var filter = function (arr, fn) { + const result = arr.filter(fn) + return result +}; \ No newline at end of file diff --git a/javascript/2635-apply-transform-over-each-element-in-array.js b/javascript/2635-apply-transform-over-each-element-in-array.js new file mode 100644 index 000000000..65b435771 --- /dev/null +++ b/javascript/2635-apply-transform-over-each-element-in-array.js @@ -0,0 +1,8 @@ +/** + * @param {number[]} arr + * @param {Function} fn + * @return {number[]} + */ +var map = function (arr, fn) { + return arr.map((element, index) => fn(element, index)); +}; \ No newline at end of file diff --git a/javascript/2703-return-length-of-arguments-passed.js b/javascript/2703-return-length-of-arguments-passed.js new file mode 100644 index 000000000..c7f18af22 --- /dev/null +++ b/javascript/2703-return-length-of-arguments-passed.js @@ -0,0 +1,11 @@ +/** + * @param {...(null|boolean|number|string|Array|Object)} args + * @return {number} + */ +var argumentsLength = function (...args) { + return args.length; +}; + +/** + * argumentsLength(1, 2, 3); // 3 + */ \ No newline at end of file diff --git a/javascript/2704-to-be-or-not-to-be.js b/javascript/2704-to-be-or-not-to-be.js new file mode 100644 index 000000000..2f718b897 --- /dev/null +++ b/javascript/2704-to-be-or-not-to-be.js @@ -0,0 +1,21 @@ +/** + * @param {string} val + * @return {Object} + */ +var expect = function (val) { + return { + toBe: (val2) => { + if (val !== val2) throw new Error("Not Equal"); + else return true; + }, + notToBe: (val2) => { + if (val === val2) throw new Error("Equal"); + else return true; + } + } +}; + +/** + * expect(5).toBe(5); // true + * expect(5).notToBe(5); // throws "Equal" + */ \ No newline at end of file