finding the sum of two arrays elements that is equal to the input

LorenzoDev

trying to iterate over an array to find two array items whose sum equals the target, not sure why isnt working

const numsArr = [2,7,11,15]
const numTarget = 9

const  twoSum = function(nums, target) {
    const first = 0;
    const second = 0;
    for(i=0; i<=nums.length; i++){
        let tester = nums[i];
        if(tester + nums[i] == target){
          console.log('found it ')
            
            
        }else{
          console.log('i failed')
        }
    }
};
twoSum(numsArr, numsTarget)

KooiInc

You should always check for typo's: numTarget is not numsTarget; Furthermore, break the loop if the desired result is reached. Try something like:

const numsArr = [2, 7, 11, 15];

const sumFromTarget = (nums, target) => {
  let sum = 0;
  
  for (let i = 0; i <= nums.length; i += 1) {
    sum += nums[i];
    //  ^ add current value to sum
    if (sum === target) {
      console.log(nums.slice(0, i + 1).join(" + ") + " = " + target);
      break;
      // result is reached, no need for further processing
    } 
  }
  
  if (sum !== target) {
  //  ^ true if the loop is completed without result
    console.log(`Could not reach ${
      target} summing the consecutive values of the given array`);
  }
}

sumFromTarget(numsArr, 9);
sumFromTarget(numsArr, 17);
sumFromTarget(numsArr, 20);

Now if you want to find a certain sum from any of the values in the array, you have to do a bit more work. Something like:

console.clear();
const numsArr = [2, 7, 11, 15];

const sumFromTarget = (nums, target) => {
  let nums2 = nums.slice();
  let resultValues = [];

  for (let i = 0; i <= nums.length; i += 1) {
    const x = nums2.reduce((acc, n, ni) => {
      const runningTotal = ni > 0 ? acc[ni - 1][1] + n : n;
      
      if (runningTotal === target) {
        resultValues = nums2.slice(0, ni + 1);
      }
      
      if (n + nums[i] === target) {
        resultValues = [nums[i], n];
      }
      
      return [...acc, [n + nums[i], runningTotal]];
    }, []);

    const check = x.find(n => n[0] === target || n[1] === target);

    if (check) {
      console.log(`found a solution (${resultValues.join(" + ")})`);
      sum = target;
      break;
    }
  }

  if (!resultValues.length) {
    console.log(`Could not reach ${
      target} summing any of the values of the given array`);
  }
}

sumFromTarget(numsArr, 9);
sumFromTarget(numsArr, 17);
sumFromTarget(numsArr, 13);
sumFromTarget(numsArr, 120);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Given two unsorted arrays A and B ,finding some pair of elements whose sum (or difference) equal to a given k - by sorting only one of the arrays

From Dev

Finding common elements in two arrays

From Dev

Finding common elements in two arrays

From Dev

finding all possible sum of two arrays element

From Dev

Finding the number of common elements between two arrays

From Dev

Finding possible combination from an array elements where Sum is equal to

From Dev

Finding the sum of common elements between n number of arrays in java

From Dev

Ruby Arrays - Are there any elements where the sum of the elements to the left equal the sum of the elements to the right?

From Dev

Finding two non-subsequent elements in array which sum is minimal

From Dev

finding elements from two CGPoint arrays that contain the same x value

From Dev

Finding common elements between two arrays without any loop

From Dev

finding elements from two CGPoint arrays that contain the same x value

From Dev

Finding mutual elements between two higher-dimensional numpy arrays

From Dev

check how many elements are equal in two numpy arrays python

From Dev

Finding the sum of multiple inputted Arrays

From Dev

Java: How to sum the elements of two arrays with different lengths

From Dev

Getting sum of elements of two or more multidimensional arrays with same key in php

From Dev

Expect two elements to be equal

From Dev

Expect two elements to be equal

From Java

Finding sum of elements in Swift array

From Dev

Finding two associated indexes where the sum of two elements equals a target value

From Dev

Eliminate Cell Arrays with equal elements

From Dev

Finding sum of two fractions prolog

From Dev

Finding the amount of equal elements in the beginning of a list

From Dev

Finding the intersect of two arrays in Fortran

From Dev

Arrays with elements less or equal to the elements in given array

From Dev

Datastructure related. Find all arrays elements greater than or equal to given input

From Dev

Check if two arrays' values are equal

From Dev

Checking if two arrays are equal - C

Related Related

  1. 1

    Given two unsorted arrays A and B ,finding some pair of elements whose sum (or difference) equal to a given k - by sorting only one of the arrays

  2. 2

    Finding common elements in two arrays

  3. 3

    Finding common elements in two arrays

  4. 4

    finding all possible sum of two arrays element

  5. 5

    Finding the number of common elements between two arrays

  6. 6

    Finding possible combination from an array elements where Sum is equal to

  7. 7

    Finding the sum of common elements between n number of arrays in java

  8. 8

    Ruby Arrays - Are there any elements where the sum of the elements to the left equal the sum of the elements to the right?

  9. 9

    Finding two non-subsequent elements in array which sum is minimal

  10. 10

    finding elements from two CGPoint arrays that contain the same x value

  11. 11

    Finding common elements between two arrays without any loop

  12. 12

    finding elements from two CGPoint arrays that contain the same x value

  13. 13

    Finding mutual elements between two higher-dimensional numpy arrays

  14. 14

    check how many elements are equal in two numpy arrays python

  15. 15

    Finding the sum of multiple inputted Arrays

  16. 16

    Java: How to sum the elements of two arrays with different lengths

  17. 17

    Getting sum of elements of two or more multidimensional arrays with same key in php

  18. 18

    Expect two elements to be equal

  19. 19

    Expect two elements to be equal

  20. 20

    Finding sum of elements in Swift array

  21. 21

    Finding two associated indexes where the sum of two elements equals a target value

  22. 22

    Eliminate Cell Arrays with equal elements

  23. 23

    Finding sum of two fractions prolog

  24. 24

    Finding the amount of equal elements in the beginning of a list

  25. 25

    Finding the intersect of two arrays in Fortran

  26. 26

    Arrays with elements less or equal to the elements in given array

  27. 27

    Datastructure related. Find all arrays elements greater than or equal to given input

  28. 28

    Check if two arrays' values are equal

  29. 29

    Checking if two arrays are equal - C

HotTag

Archive