Didart

Combinations - Nested Loops

Apr 19th, 2022
816
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function combinations(input) {
  2.     let n = Number(input[0]);
  3.  
  4.     let validCombinations = 0;
  5.     for (let x1 = 0; x1 <= n; x1++) {
  6.         for (x2 = 0; x2 <= n; x2++) {
  7.             for (x3 = 0; x3 <= n; x3++) {
  8.  
  9.                 if (x1 + x2 + x3 === n) {
  10.                     validCombinations++;
  11.                 }
  12.             }
  13.         }
  14.     }
  15.     console.log(validCombinations);
  16. }
  17.  
  18. combinations(["25"])
Advertisement
Add Comment
Please, Sign In to add comment