Didart

Sum of Two Numbers - Nested Loops

Apr 19th, 2022
868
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function sumOfTwoNumbers(input) {
  2.  
  3.     let startNum = Number(input[0]);
  4.     let endNum = Number(input[1]);
  5.     let sum = Number(input[2]);
  6.  
  7.     let isFound = false;
  8.     let combination = 0;
  9.     let numberOne = 0;
  10.     let numberTwo = 0;
  11.     for (let first = startNum; first <= endNum; first++) {
  12.         for (let second = startNum; second <= endNum; second++) {
  13.             combination++;
  14.             if (first + second === sum) {
  15.                 numberOne = first;
  16.                 numberTwo = second;
  17.                 isFound = true;
  18.                 break;
  19.             }
  20.         }
  21.         if (isFound) {
  22.             break;
  23.         }
  24.     }
  25.     if (isFound) {
  26.         console.log(`Combination N:${combination} (${numberOne} + ${numberTwo} = ${sum})`);
  27.     } else {
  28.         console.log(`${combination} combinations - neither equals ${sum}`);
  29.     }
  30.  
  31. }
  32.  
  33. sumOfTwoNumbers(["1", "10", "5"])
  34.  
  35.  
  36.  
  37.  
Advertisement
Add Comment
Please, Sign In to add comment