 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Removing the second number of the pair that add up to a target in JavaScript
Problem
We are required to write a JavaScript function that takes in an array of numbers and a target sum.
Our function should remove the second number of all such consecutive number pairs from the array that add up to the target number.
Example
Following is the code −
const arr = [1, 2, 3, 4, 5]; const target = 3; const removeSecond = (arr = [], target = 1) => {    const res = [arr[0]];    for(i = 1; i < arr.length; i++){       if(arr[i] + res[res.length-1] !== target){          res.push(arr[i]);       };    };    return res; }; console.log(removeSecond(arr, target)); Output
Following is the console output −
[ 1, 3, 4, 5 ]
Advertisements
 