-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo.js
More file actions
40 lines (36 loc) · 822 Bytes
/
Copy pathdemo.js
File metadata and controls
40 lines (36 loc) · 822 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const arr=[4,6,-2,-3,5]
const result=arr.filter(function(ele){
return ele>=0
})
const res=arr.filter((ele)=>{
return ele>=0
})
const output=arr.reduce(function(preVal,currVal){
return preVal+currVal
},0)
const output2=arr.reduce((preVal,currVal)=>{
return preVal+currVal
})
function obj(arr){
let positive=[]
let negative=[]
for(let i=0;i<arr.length;i++){
if(arr[i]>=0){
positive.push(arr[i])
}
else{
negative.push(arr[i])
}
}
return {positive,negative}
}
console.log(obj(arr))
const result2=arr.reduce((preVal,currVal)=>{
if(currVal>=0){
preVal.positive.push(currVal)
}
else{
preVal.negative.push(currVal)
}
return preVal
},{positive:[],negative:[]})