leetcode

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit 39506cb26cd217009ca6a46d7967ac290cc1e2f1
parent 95babb3b94d468893fd6f56f96757bb15668b9d4
Author: AndrewLockVI <andrewlaack1@gmail.com>
Date:   Wed, 26 Apr 2023 06:22:22 -0500

Completed add digits using js

Diffstat:
Aadd-digits/add-digits.js | 24++++++++++++++++++++++++
1 file changed, 24 insertions(+), 0 deletions(-)

diff --git a/add-digits/add-digits.js b/add-digits/add-digits.js @@ -0,0 +1,24 @@ +//Add the digits of an inputted integer until +//the output is a number 1-9 then return that value. +//The time complexiy of this code is O(log(n)) where n is the input number +//The true time complexity is O(log10N) where the output would be the number +//of digits in the value, but since we are talking big O notation the constant +//does not get factored in. +//Time: 61ms Beats: 93.60% +//Memory: 43.3MB Beats: 92.76% + +var addDigits = function(num) { + while(num >= 10){ + num = add_vals(num); + } + return num; +}; + +function add_vals(num) { + let ret_val = 0; + while(num > 0){ + ret_val += num % 10; + num = Math.floor(num / 10); + } + return ret_val; +}