DEV Community

tewkesburyd
tewkesburyd

Posted on

Array Has Target Sum

Here is a fun challenge to practice nested looping.

We are going to write a function that takes two parameter, an array of numbers, and a target number.

The goal of the function is to determine if the array contains two numbers, when added together, equal the target number.

Lets start:

Let's first declare a function and set the parameters.

const hasTargetSum = (array, target) => {...}
Enter fullscreen mode Exit fullscreen mode

Now we need need to loop over the array. We will use simple a for loop. Here we need to set three expressions.

for (let i = 0; i < array.length; i++)

The first is which index position to start at. Index 0 is the first number of the array.

The second is a condition for when to stop the loop. We want i to go over all numbers in the array and stop at the last number.

The third is how the loop will move over the array. There are many ways to write this. i++ is stating that we will increase i by 1 after each loop.

Here are all the current steps together.

const hasTargetSum = (array, target) => {
  for (let i = 0; i < array.length; i++){
    ....
  }
}
Enter fullscreen mode Exit fullscreen mode

We know we have to add two numbers in the array together. To do this, we will need to loop over the array again. We can use another for loop, but this one will be slightly different that the previous for loop.

const hasTargetSum = (array, target) => {
  for (let i = 0; i < array.length; i++){
    for (let n = 1; n < array.length; n++){
      ....
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Here we will start at index i+1. The reason we set n to i+1 is because we want to stay one index position ahead of i. This will prevent iterating over the same numbers. The rest of the for loop will be the same.

Now we need to write an expression for adding two numbers in the array and to see if their sums equal the target. We also need to determine what happens if that expression is true or false. Remember, we want both loops to run completely before we determine if the statement is false.

const hasTargetSum = (array, target) => {
  for (let i = 0; i < array.length; i++){
    for (let n = 1; n < array.length; n++){
      if(array[i]+array[n] === target){
        return true
    }
  }
return false
}
Enter fullscreen mode Exit fullscreen mode

Let's try an example and explain what's going on.

const array = [1, 3, 8, 10, 5, 3]
const target = 15

const hasTargetSum = (array, target) => {
  for (let i = 0; i < array.length; i++){
    for (let n = i+1; n < array.length; n++){
      if(array[i]+array[n] === target){
        return true
      }
    }
  }
  return false
}
Enter fullscreen mode Exit fullscreen mode

In the example, the first loop starts at index 0 (the number 1). Then the second for loop is initiated and starts at index 1 (the number 3). The if statement then checks if the sum of those numbers equals the target, 6. Since it does not, i moves to the next index number. The reason n does not change yet is because the second for loop is nested and will continue to run until it reaches the point it is told to stop (n < array.length).

Here is what's happening:
1+3
1+8
1+10
1+5
1+3

Since none of these sums equals the target, it moves back to the first for loop and moves the start index forwards one. Then the process repeats.

None of these sums equal the target. The pattern repeats:
3+8
3+10
3+5
3+3

None of these sums equal the target. The pattern repeats:
8+10
8+5
8+3

Still none. The pattern repeats, until:
10+5

Now that we have two numbers, that when added, equal the target. "true" gets returned.

Top comments (0)