Created
September 30, 2022 10:04
-
-
Save NhanKhangg98/86b7f4d6d603304b97a65a626a7659e4 to your computer and use it in GitHub Desktop.
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
| // 1 | |
| const indexOf = (arr, item) => arr.indexOf(item); | |
| indexOf([1,2,3,4], 3) | |
| indexOf([5,6,7,8], 8) | |
| indexOf([1,2,3,4], 5) | |
| // 2 | |
| const sum = (arr) => { | |
| return arr.reduce((curr, next) => curr + next); | |
| } | |
| // 3 | |
| const remove = (arr, item) => { | |
| return arr.filter(data => data != item); | |
| } | |
| // 4 | |
| const append = (arr, item) => { | |
| arr.push(item); | |
| return arr; | |
| } | |
| //5 | |
| const truncate = (arr) => { | |
| arr.pop(); | |
| return arr; | |
| } | |
| // 6 | |
| const prepend = (arr, item) => { | |
| arr.unshift(item) | |
| return arr; | |
| } | |
| // 7 | |
| const curtail = (arr) => { | |
| arr.shift(); | |
| return arr; | |
| } | |
| // 8 | |
| const insert = (arr, item, index) => { | |
| arr.splice(index, 0, item) | |
| return arr; | |
| } | |
| // 9 | |
| const count = (arr, item) => { | |
| return arr.filter(data => data == item).length; | |
| } | |
| // 10 | |
| const duplicates = (arr) => { | |
| const result = []; | |
| for(let i = 0; i < arr.length; i++) { | |
| const value = Math.abs(arr[i]); | |
| if(arr[value - 1] < 0) { | |
| result.push(value); | |
| } else { | |
| arr[value - 1] *= -1; | |
| } | |
| } | |
| return result; | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment