Spocoman

09. Sum of Two Numbers

Jan 1st, 2022 (edited)
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function sumOfTwoNumbers(input) {
  2.     let firstNum = Number(input[0]);
  3.     let secondNum = Number(input[1]);
  4.     let magicNum = Number(input[2]);
  5.     let combinations = 0;
  6.     let isFound = false;
  7.  
  8.     for (let i = firstNum; i <= secondNum; i++) {
  9.         for (let j = firstNum; j <= secondNum; j++) {
  10.             combinations++;
  11.             if (i + j === magicNum) {
  12.                 console.log(`Combination N:${combinations} (${i} + ${j} = ${magicNum})`);
  13.                 isFound = true;
  14.                 break;
  15.             }
  16.         }
  17.         if (isFound) {
  18.             break;
  19.         }
  20.     }
  21.     if (isFound === false) {
  22.         console.log(`${combinations} combinations - neither equals ${magicNum}`);
  23.     }
  24. }
  25.  
  26. РЕШЕНИЕ С PROCESS.EXIT(0):
  27.  
  28. function sumOfTwoNumbers(input) {
  29.     let firstNum = Number(input[0]);
  30.     let secondNum = Number(input[1]);
  31.     let magicNum = Number(input[2]);
  32.     let combinations = 0;
  33.  
  34.     for (let i = firstNum; i <= secondNum; i++) {
  35.         for (let j = firstNum; j <= secondNum; j++) {
  36.             combinations++;
  37.             if (i + j === magicNum) {
  38.                 console.log(`Combination N:${combinations} (${i} + ${j} = ${magicNum})`);
  39.                 return process.exit(0); // ПРЕКЪСВА ИЗПЪЛНЕНИЕТО НА ПРОГРАМАТА
  40.             }
  41.         }
  42.     }
  43.     console.log(`${combinations} combinations - neither equals ${magicNum}`);
  44. }
Add Comment
Please, Sign In to add comment