Skip to content

Instantly share code, notes, and snippets.

@NhanKhangg98
Created September 30, 2022 08:15
Show Gist options
  • Save NhanKhangg98/affb8820541f04f3ddad08f44788520c to your computer and use it in GitHub Desktop.
Save NhanKhangg98/affb8820541f04f3ddad08f44788520c to your computer and use it in GitHub Desktop.
- [ ] Test your concepts of arrays by determining the location of an item in an array.
```js
const indexOf = (arr, item) => {
// Your code here
}
```
```js
indexOf([1,2,3,4], 3) => 2
indexOf([5,6,7,8], 8) => 3
indexOf([1,2,3,4], 5) => -1
```
---
- [ ] Evaluate your concepts by populating an array.
```js
const sum = (arr) => {
// Your code here
}
```
```js
sum([1,2,3,4]) => 10
sum([5,6,7,8]) => 26
sum([-1,-2,1,2]) => 0
```
---
- [ ] Evaluate yourself by returning a new array after removing all instances of a value from an array.
```js
// Returns a new array
const remove = (arr, item) => {
// Your code here
}
```
```js
remove([1,2,3,4,2], 2) => [1,3,4]
remove([5,6,7,8,6], 6) => [5,7,8]
remove([1], 1) => []
```
---
- [ ] Evaluate yourself by adding an item to the end of an array
```js
const append = (arr, item) => {
// Your code here
}
```
```js
append([1,2,3,4], 5) => [1,2,3,4,5]
append([], 1) => [1]
```
---
- [ ] Evaluate yourself by removing the last item of an array
```js
const truncate = (arr) => {
// Your code here
}
```
```js
truncate([1,2,3,4,5]) => [1,2,3,4]
truncate([1]) => []
```
---
- [ ] Test yourself by Adding an item at the beginning of an array.
```js
// Prepends to the original array
const prepend = (arr, item) => {
// Your code here
}
```
```js
prepend([2,3,4,5], 1) => [1,2,3,4,5]
prepend([], 1) => [1]
```
---
- [ ] Test yourself by removing the first item of an array.
```js
// Modifies the original array
const curtail = (arr) => {
// Your code here
}
```
```js
curtail([1,2,3,4,5]) => [2,3,4,5]
curtail([1]) => []
```
---
- [ ] Test yourself by adding an item anywhere in an array.
```js
// Inserts an element in the original array
const insert = (arr, item, index) => {
// Your code here
}
```
```js
insert([1,2,4], 3, 2) => [1,2,3,4]
insert([2], 1, 0) => [1,2]
insert([], 1, 0) => [1]
```
---
- [ ] Test yourself by counting the occurrences of an item in an array.
```js
const count = (arr, item) => {
// Your code here
}
```
```js
count([1,2,3,2,4,2], 2) => 3
count([5,6,7,8], 2) => 0
count([], 1) => 0
```
---
- [ ] Test yourself by finding the duplicates in an array.
```js
const duplicates = (arr) => {
// Your code here
}
```
```js
duplicates([1,2,3,2,4,2,1]) => [1,2]
duplicates([5,6,7,8]) => []
duplicates([]) => []
```
---
- [ ] Test yourself by Squaring each number in an array.
```js
const square = (arr) => {
// Your code here
}
```
```js
square([1,2,3,10]) => [1,4,9,100]
square([]) => []
```
---
- [ ] Test yourself by finding all occurrences of an item in an array.
```js
const findAllOccurrences = (arr, target) => {
}
```
```js
findAllOccurrences([1,2,3,4], 3) => [2]
findAllOccurrences([5,6,7,8,6], 6) => [1,4]
findAllOccurrences([1,2,3], 4) => []
findAllOccurrences([], 1) => []
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment