const indexOf = (arr, item) => {
// Your code here
}
indexOf([1,2,3,4], 3) => 2
indexOf([5,6,7,8], 8) => 3
indexOf([1,2,3,4], 5) => -1
const sum = (arr) => {
// Your code here
}
sum([1,2,3,4]) => 10
sum([5,6,7,8]) => 26
sum([-1,-2,1,2]) => 0
// Returns a new array
const remove = (arr, item) => {
// Your code here
}
remove([1,2,3,4,2], 2) => [1,3,4]
remove([5,6,7,8,6], 6) => [5,7,8]
remove([1], 1) => []
const append = (arr, item) => {
// Your code here
}
append([1,2,3,4], 5) => [1,2,3,4,5]
append([], 1) => [1]
const truncate = (arr) => {
// Your code here
}
truncate([1,2,3,4,5]) => [1,2,3,4]
truncate([1]) => []
// Prepends to the original array
const prepend = (arr, item) => {
// Your code here
}
prepend([2,3,4,5], 1) => [1,2,3,4,5]
prepend([], 1) => [1]
// Modifies the original array
const curtail = (arr) => {
// Your code here
}
curtail([1,2,3,4,5]) => [2,3,4,5]
curtail([1]) => []
// Inserts an element in the original array
const insert = (arr, item, index) => {
// Your code here
}
insert([1,2,4], 3, 2) => [1,2,3,4]
insert([2], 1, 0) => [1,2]
insert([], 1, 0) => [1]
const count = (arr, item) => {
// Your code here
}
count([1,2,3,2,4,2], 2) => 3
count([5,6,7,8], 2) => 0
count([], 1) => 0
const duplicates = (arr) => {
// Your code here
}
duplicates([1,2,3,2,4,2,1]) => [1,2]
duplicates([5,6,7,8]) => []
duplicates([]) => []
const square = (arr) => {
// Your code here
}
square([1,2,3,10]) => [1,4,9,100]
square([]) => []
const findAllOccurrences = (arr, target) => {
}
findAllOccurrences([1,2,3,4], 3) => [2]
findAllOccurrences([5,6,7,8,6], 6) => [1,4]
findAllOccurrences([1,2,3], 4) => []
findAllOccurrences([], 1) => []
//Exercise 1
1.
const findIndex = (arr, item) => {
return arr.indexOf(item);
};
console.log(findIndex([1, 2, 3, 4], 3))
console.log(findIndex([5, 6, 7, 8], 8))
console.log(findIndex([1, 2, 3, 4], 5))
const sum = (arr) => {
return arr.reduce((a, b) => a + b, 0);
};
console.log(sum([1, 2, 3, 4]), sum([5, 6, 7, 8]), sum([-1, -2, 1, 2]));
const remove = (arr, item) => {
const result = arr.filter((num) => num !== item)
console.log(result);
}
remove([1,2,3,4,2], 2)
remove([5,6,7,8,6], 6)
remove([1], 1)
const append = (arr, item) => {
arr.push(item);
return arr;
};
console.log(append([1, 2, 3, 4], 5), append([], 1));
const truncate = (arr) => {
arr.pop();
return arr
}
console.log(truncate([1,2,3,4,5]),
truncate([1]))
const curtail = (arr) => {
arr.shift()
return arr
}
console.log((curtail([1,2,3,4,5])),(curtail([1])))
const insert = (arr, item, index) => {
arr.splice(index, 0, item);
return arr;
};
console.log(insert([1, 2, 4], 3, 2), insert([2], 1, 0), insert([], 1, 0));
const count = (arr, item) => {
let count = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[i] == item) count++;
}
return count
};
console.log(count([1, 2, 3, 2, 4, 2], 2), count([5, 6, 7, 8], 2), count([], 1));
const duplicates = (arr) => {
const count = new Set();
return arr.filter(num => count.size === count.add(num).size);
};
console.log(
duplicates([1, 2, 3, 2, 4, 2, 1]), duplicates([5, 6, 7, 8]), duplicates([])
);
const square = (arr) => {
for (let i = 0; i < arr.length; i++) {
console.log(Math.pow(arr[i], 2))
}
};
square([1, 2, 3, 10])
square([]);