Spocoman

08. Equal Pairs

Dec 26th, 2021 (edited)
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function equalPairs(input) {
  2.     let num = Number(input[0]);
  3.     let value = Number(input[1]) + Number(input[2]);
  4.     let maxdiff = 0;
  5.  
  6.     for (let i = 3; i < num * 2; i++){
  7.         let current = Number(input[i++]) + Number(input[i]);
  8.         let diff = Math.abs(value - current);
  9.         if (diff > maxdiff){
  10.             maxdiff = diff;
  11.         } else{
  12.             value = current;
  13.         }
  14.     }
  15.     if (maxdiff === 0){
  16.         console.log(`Yes, value=${value}`);
  17.     } else{
  18.         console.log(`No, maxdiff=${maxdiff}`);
  19.     }
  20. }
  21.  
  22.  
  23. РЕШЕНИЕ С ТЕРНАРЕН ОПЕРАТОР:
  24.  
  25. function equalPairs(input) {
  26.     let num = Number(input[0]);
  27.     let value = Number(input[1]) + Number(input[2]);
  28.     let maxdiff = 0;
  29.  
  30.     for (let i = 3; i < num * 2; i++){
  31.         let current = Number(input[i++]) + Number(input[i]);
  32.         let diff = Math.abs(value - current);
  33.         diff > maxdiff ? maxdiff = diff : value = current;  
  34.     }
  35.    
  36.     console.log(maxdiff === 0 ? `Yes, value=${value}` : `No, maxdiff=${maxdiff}`);
  37. }
  38.  
Add Comment
Please, Sign In to add comment