DEV Community

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

Posted on • Edited 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

SurveyJS custom survey software

Simplify data collection in your JS app with a fully integrated form management platform. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more. Integrates with any backend system, giving you full control over your data and no user limits.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs