Day 21 of JavaScriptmas - Sum of Two Solution

flyingduck92

Sekti Wicaksono

Posted on December 21, 2020

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
💖 💪 🙅 🚩
flyingduck92
Sekti Wicaksono

Posted on December 21, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Day 21 of JavaScriptmas - Sum of Two Solution
24daysofjavascriptmas Day 21 of JavaScriptmas - Sum of Two Solution

December 21, 2020

Day 20 of JavaScriptmas - Domain Type Solution
24daysofjavascriptmas Day 20 of JavaScriptmas - Domain Type Solution

December 20, 2020