DEV Community

Cover image for Day 21 of JavaScriptmas - Sum of Two Solution
Sekti Wicaksono
Sekti Wicaksono

Posted on • Updated on

Day 21 of JavaScriptmas - Sum of Two Solution

Day 21 Challenge is to find out does the value is the sum of 2 values that coming from 2 different arrays.

For instance,
Does 42 is the sum of 2 (that in array_1) and 40 (that in array_2)?
If it does then return true, because 42 can be produced from number 2 and 40 that coming from those two array. If not just return false

Today, I tested it with

const nums1 = [1, 2, 3];
const nums2 = [10, 20, 30, 40];
const value = 42; 
Enter fullscreen mode Exit fullscreen mode

This is the JavaScript Solution

function sumOfTwo(nums1, nums2, value) {   

    let sum = value;

    for(let i=0; i < nums1.length; i++) {
        for(let j=0; j < nums2.length; j++) {
            // console.log(`${nums1[i]} + ${nums2[j]} is ${nums1[i] + nums2[j]}`);  

            // return true if FOUND  
            if(sum === nums1[i] + nums2[j]) {
                return true;
            }   
        }
    }

    // return false if NOT found 
    return false;
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)