Skip to content

Commit 2d03378

Browse files
committed
added some challenging datagit add .git add .!
0 parents  commit 2d03378

File tree

7 files changed

+128
-0
lines changed

7 files changed

+128
-0
lines changed

ArrReduce.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Given an integer array nums, a reducer function fn, and an initial value init, return the final result obtained by executing the fn function on each element of the array, sequentially, passing in the return value from the calculation on the preceding element.
2+
3+
// This result is achieved through the following operations: val = fn(init, nums[0]), val = fn(val, nums[1]), val = fn(val, nums[2]), ... until every element in the array has been processed. The ultimate value of val is then returned.
4+
5+
// If the length of the array is 0, the function should return init.
6+
7+
// Please solve it without using the built-in Array.reduce method.
8+
9+
var reduce = function (nums, fn, init) {
10+
let val = init;
11+
if (nums.length === 0) {
12+
return init
13+
} else {
14+
nums.forEach((num) => {
15+
val = fn(val, num)
16+
})
17+
}
18+
return val;
19+
};

Array.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Given an integer array arr and a mapping function fn, return a new array with a transformation applied to each element.
2+
3+
// The returned array should be created such that returnedArray[i] = fn(arr[i], i).
4+
5+
// Please solve it without the built-in Array.map method.
6+
7+
var map = function (arr, fn) {
8+
var result = [];
9+
arr.forEach((value, index) => {
10+
result.push(fn(value, index));
11+
})
12+
return result
13+
}

Counter!!.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Write a function createCounter. It should accept an initial integer init. It should return an object with three functions.
2+
3+
// The three functions are:
4+
5+
// increment() increases the current value by 1 and then returns it.
6+
// decrement() reduces the current value by 1 and then returns it.
7+
// reset() sets the current value to init and then returns it.
8+
9+
var createCounter = function (init) {
10+
let currrentValue = init
11+
return {
12+
increment: () => {
13+
return ++currrentValue;
14+
},
15+
decrement: () => {
16+
return --currrentValue;
17+
},
18+
reset: () => {
19+
currrentValue = init;
20+
return currrentValue;
21+
}
22+
}
23+
}

FilterArray.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Given an integer array arr and a filtering function fn, return a filtered array filteredArr.
2+
3+
// The fn function takes one or two arguments:
4+
5+
// arr[i] - number from the arr
6+
// i - index of arr[i]
7+
// filteredArr should only contain the elements from the arr for which the expression fn(arr[i], i) evaluates to a truthy value. A truthy value is a value where Boolean(value) returns true.
8+
9+
// Please solve it without the built-in Array.filter method.
10+
11+
12+
// ! the filter function is with the For Loop method
13+
/*var filter=function(arr,fn){
14+
var filteredArr=[];
15+
for(var i=0;i<arr.length;i++){
16+
if(fn(arr[i],i)){
17+
filteredArr.push(arr[i]);
18+
}
19+
}
20+
return filteredArr;
21+
}*/
22+
// ? the filter function is with the ForEach method
23+
var filter = function (arr, fn) {
24+
let newArr = []
25+
arr.forEach((value, index) => {
26+
if (fn(value, index)) {
27+
newArr.push(value);
28+
}
29+
})
30+
return newArr;
31+
}

ToBe.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function expect(val){
2+
return {
3+
toBe:(value)=>{
4+
if(value===val){
5+
return true;
6+
}else{
7+
throw "Not Equal";
8+
}
9+
},
10+
notToBe:(value)=>{
11+
if(value !== val){
12+
return true;
13+
}else{
14+
throw "Equal "
15+
}
16+
}
17+
18+
}
19+
}
20+
const result =expect(5).toBe(5)
21+
console.log(result)

counter.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function createCounter(n) {
2+
let count = n;
3+
return function () {
4+
return count++;
5+
}
6+
}
7+
8+
const counter = createCounter(10);
9+
const result = [
10+
counter(),
11+
counter(),
12+
counter()
13+
]
14+
console.log(result)

helloWorld.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function createHellowWorld() {
2+
return function () {
3+
return "hello world";
4+
}
5+
}
6+
const helloWorld=createHellowWorld();
7+
console.log(helloWorld())

0 commit comments

Comments
 (0)