SaveText.Ru

[email protected]# hw recursion
  1. 'use strict';
  2.  
  3. function sum(number) {
  4.     const integer = parseInt(number);
  5.     if (isNaN(integer)) {
  6.         return `Wrong argument '${number}'. Argument must be integer.`;
  7.     }
  8.     const absInteger = Math.abs(integer);
  9.     if (absInteger <= 0) {
  10.         return 0;
  11.     }
  12.     const lastDigit = absInteger % 10;
  13.     return lastDigit + sum((absInteger - lastDigit) / 10);
  14. }
  15.  
  16. console.log(sum('-257'));

Share with your friends:

Распечатать