Created
August 4, 2016 14:49
-
-
Save rajivnarayana/67cc8741ffa31c74e6dde9a3a728af8f to your computer and use it in GitHub Desktop.
Array manipulations in Javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let list = [9,5,5,0,2,5,9,5,6,7]; | |
//find if any of the number is greater than 6 | |
//find the sum of all numbers | |
//find the number of even numbers and number of odd numbers | |
//find how many times each number repeats | |
//find an array where each number is the squares the corresponding number in the first array. | |
//find an array where the character representing the number repeats the number of times of the corresponding number | |
// ex : [2, 3, 1] = ['c','c','d','d','d','b'] | |
var result = list.some(function(element) { | |
return element > 10; | |
}) | |
console.log(`Problem 1 result : ${result}`); | |
let sum=0; | |
for (var i=0; i<list.length; i++) { | |
sum += list[i]; | |
} | |
console.log(`Problem 2 Method 1 sum: ${sum}`); | |
sum=0; | |
list.forEach((element) => { | |
sum+=element; | |
}) | |
console.log(`Problem 2 Method 2 sum: ${sum}`); | |
console.log(`Problem 2 Method 3: Sum ${list.reduce((prev, current) => prev+current, 0)}`); | |
//find the number of even numbers and number of odd numbers | |
// let evenNumbers = list.filter(function(element) { | |
// return element % 2 == 0; | |
// }) | |
// let oddNumbers = list.filter(function(element) { | |
// return element % 2 == 1; | |
// }) | |
// let differentOddNumbers = oddNumbers.reduce(function(prev, current){ | |
// if (prev.indexOf(current) == -1) { | |
// prev.push(current); | |
// } | |
// return prev; | |
// },[]); | |
let result1 = list.reduce(function(prev, current) { | |
if (current % 2 ==0) { | |
if (prev.even.indexOf(current) == -1) { | |
prev.even.push(current); | |
} | |
} else { | |
if (prev.odd.indexOf(current) == -1) { | |
prev.odd.push(current); | |
} | |
} | |
return prev; | |
}, { | |
even : [], | |
odd: [] | |
}) | |
console.log(`Problem 3 Odd ${result1.odd}, even ${result1.even}`); | |
//find an array where each number is the squares the corresponding number in the first array. | |
var newArray = []; | |
for(var i=0; i<list.length; i++) { | |
newArray.push(list[i] * list[i]); | |
} | |
console.log(`Problem 4 ${list.map(function(element) { | |
return element * element; | |
})}`); | |
list.map(element => element * element) | |
let x = element => element * element | |
let y = function(element) { | |
return element * element; | |
} | |
//find how many times each number repeats | |
let result3 = list.reduce((prev, element) => { | |
if (prev[element]) { | |
prev[element] = prev[element] +1; | |
} else { | |
prev[element] = 1; | |
} | |
return prev; | |
},{}); | |
console.log(`Result: ${JSON.stringify(result3)}`); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment