Javascript logic practice exercises - 1

Introduction

In this assignment, you will learn about some common array operations in JavaScript.

Remove Duplicate elements from an array in JavaScript - Problem 1

Write a function that takes an array as input and returns a new array with all duplicate elements removed. Ensure that the order of the elements in the output array remains the same as in the original array.

1
2
3
4
5
var arr = [1, 2, 5, 2, 1, 8];
function removeDuplicates(array) {
  // Your logic here
}
console.log(removeDuplicates(arr)); // Output: [1, 2, 5, 8]

Finding Negative Numbers in an Array - Problem 2

Write a function that takes an array of numbers as input and returns an array containing only the negative numbers from the input array.

1
2
3
4
5
var arr = [1, 2, -3, 4, -5];
function findNegativeNumbers(array) {
  // Your logic here
}
console.log(findNegativeNumbers(arr)); // Output: [-3, -5]

Sum of All Numbers in an Array - Problem 3

Write a function that takes an array of numbers as input and returns the sum of all the numbers in the array.

1
2
3
4
5
var arr = [1, 2, 3, 4, 5];
function sumOfAllNumbers(array) {
  // Your logic here
}
console.log(sumOfAllNumbers(arr)); // Output: 15

Sum of Even Numbers in an Array - Problem 4

Write a function that takes an array of numbers as input and returns the sum of all the even numbers in the array.

1
2
3
4
5
var arr = [1, 2, 3, 4, 5];
function sumOfEvenNumbers(array) {
  // Your logic here
}
console.log(sumOfEvenNumbers(arr)); // Output: 6

Sum of Odd Numbers in an Array - Problem 5

Write a function that takes an array of numbers as input and returns the sum of all the odd numbers in the array.

1
2
3
4
5
var arr = [1, 10, 5, 3, 7];
function sumOfOddNumbers(array) {
  // Your logic here
}
console.log(sumOfOddNumbers(arr)); // Output: 16

Converting Array into Object by Counting the Number of Times an Element Repeats - Problem 6

Develop a function that transforms an array into an object where the keys are the unique elements from the array, and the values are the counts of how many times each element appears in the array.

1
2
3
4
5
var arr = ["apple", "banana", "apple", "orange", "banana", "apple"];
function countElements(array) {
  // Your logic here
}
console.log(countElements(arr)); // Output: {apple: 3, banana: 2, orange: 1}

Find the Largest Number in an Array - Problem 7

Write a function that takes an array of numbers as input and returns the largest number in the array.

1
2
3
4
5
var arr = [10, 5, 3, 8, 2, 30, 20];
function findLargestNumber(array) {
  // Your logic here
}
console.log(findLargestNumber(arr)); // Output: 30

Find the Smallest Number in an Array - Problem 8

Write a function that takes an array of numbers as input and returns the smallest number in the array.

1
2
3
4
5
var arr = [10, 5, 3, 8, 2, 30, 20];
function findSmallestNumber(array) {
  // Your logic here
}
console.log(findSmallestNumber(arr)); // Output: 2

Merge Two Arrays and Remove Duplicate Entries - Problem 9

Develop a function that merges two arrays and removes duplicate entries.

1
2
3
4
5
6
var arr1 = [1, 2, 3, 4, 5];
var arr2 = [3, 4, 5, 6, 7];
function mergeAndRemoveDuplicates(arr1, arr2) {
  // Your logic here
}
console.log(mergeAndRemoveDuplicates(arr1, arr2)); // Output: [1, 2, 3, 4, 5, 6, 7]

Counting Truthy Values in an Array - Problem 10

Develop a function that counts the number of truthy values in an array.

1
2
3
4
5
var arr = [true, false, null, undefined, 1, 0, NaN, ""];
function countTruthyValues(array) {
  // Your logic here
}
console.log(countTruthyValues(arr)); // Output: 2

Conclusion

These are some of the most popular JavaScript coding challenges. I hope you have fun and learn something new.

updatedupdated2024-04-162024-04-16
comments powered by Disqus