How to write a function that returns array elements larger than a number in JavaScript ?
Last Updated :
15 Feb, 2023
Given an array arr and number n and the task is to write a function that returns an array whose items are larger than n.
Example:
Input: arr = [65, 16, 0, 6, 64, 1, 68]
n = 16
Output: [65, 64, 68]
Input: arr = [6, 46, 54, 6, 56, 54, 65, 4, 65]
n = 50
Output: [54, 56, 54, 65, 65]
To achieve this we have the following approaches :
Approach 1: Using Array.filter()
In this approach, we use the Array.filter method. At every iteration, we check if the value is greater than num or not.
Example:
JavaScript
<script>
let returnLarger = (arr, num) => arr.filter(n => n > num);
console.log(returnLarger( [65, 16, 0, 6, 64, 1, 68], 16))
console.log(returnLarger([6, 46, 54, 6, 56, 54, 65, 4, 65], 50))
</script>
Output:
[65, 64, 68]
[54, 56, 54, 65, 65]
Approach 2: Using Array.reduce()
In this approach, we use the array.reduce method. Initialize the accumulator using an empty array and at every iteration check if the current value is greater than num then concatenate the current value with the accumulator and return it otherwise return the accumulator as it is.
Example:
JavaScript
<script>
// Creating a function that return an array
// whose items are less than num
let returnLarger = (arr, num) => {
return arr.reduce((acc, curr)=>{
if(curr > num){
return acc.concat(curr) // Concatenate the acc with arr
}else{
return acc
}
}, []) // Initialize the accumulator with an empty array
}
console.log(returnLarger( [65, 16, 0, 6, 64, 1, 68], 16))
console.log(returnLarger([6, 46, 54, 6, 56, 54, 65, 4, 65], 50))
</script>
Output:
[65, 64, 68]
[54, 56, 54, 65, 65]
Approach 3: Using the Array.map method:
In this approach, we iterate the array using the map method and at every iteration, we check if the current value is greater than num or not? If true, then we return the value as it. If false, then replace the item with an empty string (Or any falsy value). After that using the filter method remove the falsy values.
JavaScript
<script>
// Creating a function that return an array
// whose items are less than num
let returnLarger = (arr, num) => {
return arr.map(v => v > num ? v : "").filter(Boolean)
}
console.log(returnLarger( [65, 16, 0, 6, 64, 1, 68], 16))
console.log(returnLarger([6, 46, 54, 6, 56, 54, 65, 4, 65], 50))
</script>
Output:
[65, 64, 68]
[54, 56, 54, 65, 65]
Approach 4: using the for loop:
In this approach, we create an empty array and iterate the given array using the for loop and at every iteration, we check the current value is greater than num or not. If true, then we push the value in the newly created array.
JavaScript
<script>
// Creating a function that return an array
// whose items are less than num
let returnLarger = (arr, num) => {
let newArr = []
for(let i = 0; i < arr.length; i++){
if(arr[i] > num){
newArr.push(arr[i])
}
}
return newArr
}
console.log(returnLarger( [65, 16, 0, 6, 64, 1, 68], 16))
console.log(returnLarger([6, 46, 54, 6, 56, 54, 65, 4, 65], 50))
</script>
Output:
[65, 64, 68]
[54, 56, 54, 65, 65]
Approach 5: using the forEach loop:
In this approach, we create an empty array and iterate the given array using forEach loop and at every iteration, we perform expression with and operator where first operator checks the current value is greater than num or not if it return false new operation is not performed and if return true then it push the element to array.
JavaScript
<script>
// Creating a function that return an array
// whose items are less than num
let returnLarger = (arr, num) => {
let newArr = []
arr.forEach( ele => (ele > num) && newArr.push(ele))
return newArr
}
console.log(returnLarger( [65, 16, 0, 6, 64, 1, 68], 16))
console.log(returnLarger([6, 46, 54, 6, 56, 54, 65, 4, 65], 50))
</script>
Output:
[ 65, 64, 68 ]
[ 54, 56, 54, 65, 65 ]
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
JavaScript Interview Questions and Answers JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as
15+ min read
React Tutorial React is a JavaScript Library known for front-end development (or user interface). It is popular due to its component-based architecture, Single Page Applications (SPAs), and Virtual DOM for building web applications that are fast, efficient, and scalable.Applications are built using reusable compon
8 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read