Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created Files 1941,2626,2629,2634,2635,2703 and 2704 in Javascript #3332

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -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
};
22 changes: 22 additions & 0 deletions javascript/2626-array-reduce-transformation.js
Original file line number Diff line number Diff line change
@@ -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
};
22 changes: 22 additions & 0 deletions javascript/2629-function-composition.js
Original file line number Diff line number Diff line change
@@ -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
*/
9 changes: 9 additions & 0 deletions javascript/2634-filter-elements-from-array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* @param {number[]} arr
* @param {Function} fn
* @return {number[]}
*/
var filter = function (arr, fn) {
const result = arr.filter(fn)
return result
};
8 changes: 8 additions & 0 deletions javascript/2635-apply-transform-over-each-element-in-array.js
Original file line number Diff line number Diff line change
@@ -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));
};
11 changes: 11 additions & 0 deletions javascript/2703-return-length-of-arguments-passed.js
Original file line number Diff line number Diff line change
@@ -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
*/
21 changes: 21 additions & 0 deletions javascript/2704-to-be-or-not-to-be.js
Original file line number Diff line number Diff line change
@@ -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"
*/