Skip to content

Commit

Permalink
[Exercises 16] Total Digit Rekursif
Browse files Browse the repository at this point in the history
  • Loading branch information
UzaiSindiko committed Jul 24, 2019
1 parent c4c5f28 commit d3e8b70
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions exercise-16.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function totalDigitRekursif(angka) {
// jika input kosong
if (angka == null) {
return 0;
}

angka = angka.toString();
if (angka == false) {
return 0;
}
else {
return totalDigitRekursif(Number(angka.slice(1))) + Number(angka[0]);
}
}

// TEST CASES
console.log(totalDigitRekursif(512)); // 8
console.log(totalDigitRekursif(1542)); // 12
console.log(totalDigitRekursif(5)); // 5
console.log(totalDigitRekursif(21)); // 3
console.log(totalDigitRekursif(11111)); // 5

0 comments on commit d3e8b70

Please sign in to comment.