Edabit Algorithm Add Two Large IntegersRecursion: Sum of Two Numbers (With A Twist!)

Recursion: Sum of Two Numbers (With A Twist!)

Instructions

This is an “expert” challenge!!! Why is a sum of two numbers an “expert” challenge!!! Well, the numbers can have 1000 digits or even beyond such count…

So, what’s the twist? You have to do the summation as if you’re doing it manually on a piece of paper, thus, the conversion of the numeric string to numeric literal is basically disallowed.

Examples
sum("12132000", "12171979") // "24303979"

sum("4666", "544") // "5210"

sum("1521512512512512515", "898989898989988998899898") // "898991420502501511412413"

sum("5125515215521515", "125261616261626") // "5250776831783141"

sum("666666666666666666666666666", "99999999999999999999999") // "6666766666666666666666666665"

sum("123456789123456789123456789", "987654321987654321987654329876543") // "987654445444443445444443453333332"
Notes
  • Remember how to sum two numbers ON A PAPER, such is the process.
  • Your function must run in less than 10 seconds because it has a time limit.
  • The use of Number class such as BigInt is disallowed – it will defeat the purpose and the level of difficulty associated to this challenge.
  • You are expected to solve this challenge recursively.
Solutions
const sum = (a, b,c=0) => {<!-- -->
    if(!a & amp; & amp; !b & amp; & amp; c == 0){<!-- -->
        return ''
    }
    let aa = (a)?(a.slice(-1)-0):0;
    let bb = (b)?(b.slice(-1)-0):0;
    let s = aa + bb + c
    c = s>9?1:0;
    return sum(a.slice(0,-1),b.slice(0,-1),c) + (s )
}
TestCases
let Test = (function(){<!-- -->
    return {<!-- -->
        assertEquals:function(actual,expected){<!-- -->
            if(actual !== expected){<!-- -->
                let errorMsg = `actual is ${<!-- -->actual},${<!-- -->expected} is expected`;
                throw new Error(errorMsg);
            }
        },
        assertNotEquals:function(actual,expected){<!-- -->
            if(actual === expected){<!-- -->
                let errorMsg = `actual is ${<!-- -->actual},${<!-- -->expected} is expected,they are equals`;
                throw new Error(errorMsg);
            }
        }

    }
})();

const isRecursive = src => (`${<!-- -->src}`.match(RegExp(`${<!-- -->src.name}`, 'gm'))||[] ).length > + (/function/.test(src))
Test.assertNotEquals(isRecursive(sum), false, "Recursion is required!")

let [actualParam, expectedParam] = [
    [
        ["12132000", "12171979"], ["4666", "544"], ["1521512512512512515", "898989898989988998899898"],
        ["5125515215521515", "125261616261626"], ["66666666666666666666666666", "9999999999999999999999999"],
        ["123456789123456789123456789", "987654321987654321987654329876543"], ["51", "512"],
        ["46580672134861691487886856201063433530317493541984174240640117078384844027455455145995264175402994424834479825796316174329 467969102257360195385044875023188313698661902232816682563450684527972706431205", "20129647448213526330992199933412026717951269 05987588021348946707433536804737134220772457993120823103296976004395681149470438019884837735571898476172373008767343939415905 4420344427904875384087249296946"],
        ["91282428163917923903673943182386091549299625501338276578860348289792944130334503071737934509247621432019913002881274087634 21237279633517929936847079257713141254694944681428142978110027357322312404627593110196423560326537881370897768020382035189644 68025682465917134851520867133952937086629692970216764716303851957633108401982210330975537456162314850852343138024525376565350 9318684179663600476971689801", "5920641803160990513445202815794518152101247319199211634010324208708552138569594568355624738331 70474060555615992535009756828916401847152577384846163657902464439185427709270781195395631956689052792598901956202026084625125 06637583308562660519852177338637820398931582785452910278903911520277670542804988700386079525194520041798105924663877366598352 03110761590431605238080432136468832137768846891022675668"],
        ["24050292702239538714424762926989391091054882494797379961190995916419743076846190252322346159955367441832937735205583340798 02885605973116383633357297841107589584866177046808005114686910433753221347499392692184399691370677862792470960010086046142158 77741407227939952306602334533927177769736177242962769599824035282086466176795284318594239823146820365557724859615346952915442 66936268924447065901465864784710200365748971482974309578528938725051499783831735126491173069337708438252812165533370751831832 34551190652150227094794750419810188186618132312225676840622851880638597406930288746009656197047770564619993935660692383037289 19440370658470713087001071767942619225473229227876367832838297660045290605245397014952299432116111343177233286868447134892239 61776582365551050940511119736023799724295560712462775", "325095584578001340821572489239453691069234585825971076627992829732994 14325101867489152494482453431986091527569819907127801971978514325573048496062254539304686542784848485721075326183780905103371 29350502749446284829766813243051745561147597707437755760379868776785290776225404056586655496252986870521187497620152269213799 90607663806708531235855461158518996817259494306205828988947635966796567103370174966185256301241921317249495167937353849538770 12134805554272494110981312910720545085389502607669244051838541600298937410166860469735980721786025686375273866096353307350361 73348028616131557834819212656065200755964989939148268195829887603089440246936304542088335439008816862473623862129272674821394 1087878476447239860986699971354976451472810499473465596980950104110861804910439253305564075282187561761831"], ["68096327639168 91310120420620586174664812635530867937840217826224568347127990297761160448320170180761000408251375466103628257285208000720825 37464797615988350347733001835828199332567752701755705897606833364286898429173948397562187084166730620439384034140534829494381 36205165508859076434925926844279845999740175327760473740955585663982177099650207933666402243738103045693013980353749050047198 99959454164388545839944663454514841628001498947789419801699846006819006975473515954356883318037820103882153723720601881130291 35469718447199996771668768621847239268626665031856802627351711560914399212977368328972884220847147666316197311534322384642898 87024560383878149848251503100100620428778066404807105255282413284720953249301066989172273536214069805301107470552571013505405 14070478171412772633866196018240727864894180766177622426511795549818029880640303307250349733954619463752541929370662897223878 85804860730 00341176464481569509088314164648516198308588306341581886828084652373143824178061446100845207932260299989735098857835077503613 94479267125872369914405308257871873845210208842593022368000166711535879080651348070599037264562562098520106478949967908038229 02295103535199004344579513541463510173564970382016766850105332955448103363264608782346360453065932837169557702146548335694072 50862467764241301491075773878798755773232056371316349556337923582112418745691372603517681014116333538448075872043025194944464 91737434387088903260169641817753864635782826372764228605225153915043813868714311693485202217830677534062280899299230318380230 94932975838186275232536117818506092084642936665644687739805789951821198125850941202273229487946469163874783171462224034287685 33848504528795446827274517795896686680232498391880209937677390643332411043134407239601699748922010617022901854909440001914242 164998598960486713118948710525156592829968225269277615537170861633579862039532587858207573589726753466950083211842"]
    ], [
        "24303979",
        "5210",
        "898991420502501511412413",
        "5250776831783141",
        "6666766666666666666666666665",
        "987654445444443445444443453333332",
        "563",
        "667103195830752178188790561344754602482687626018600544541295841527202120748267973537198441066112254578042398697531276690338 48167950634715914369806598753275987138056061287237026991355559912059955728151",
        "150488846195527829038125971340331273070312098693330392918963590376878465516030448755294181892564668838075474602134775063317 10401298105043703785308715836737785646549221774135954932066346924212840330616612672216684406577788545129228624286434020407378 54403886455232962706049969922992052289406398398266651720164647209578308819963269577614311122145835161928502181185049184608564 5787516317432447367994365469",
        "565598511600396727965820118509347601979783410773944876239902788897191574019480577414748406424087994279244653050254904686000 00834574056736884829635232950380582391446618953801126473052885242635584768498954416306845211374911058442165211576837535799145 37793941056184813842248749395858433193614759300148883495860505090078461674029481253027710590022815240767216768748412591212716 58310325211267226118028822813287259958731636146992590953226741100053767959665406807636671803190213489733572509228733595010763 97350448121801208358114364667837862587967348808632042272324872113736335802783173621412140318669832206851946916256823221855573 90233594187796571116947022221514527693741109141237302190512249275274300161241817794246980419831110567269978015965521296268955 8757532469661912745421558989329363799577748274224606",
        "153901188369172324865849021900952629789772840470662464285241678064551752126426709049846263816162816062083405116754558387271 15120285504334770167319234883582647530412597077020445427765952947780738977735449001659790805220189965994516467288291405458629 84108442867723404313087007078634210144394683077900195647105573445273238462885411120855403642589902800268553096709402152507161 26902084744119707624272184056900369157185422532706148600578702641389761396234281192377526668461194720378974343713585519580257 66745796825595208729041055361032569413105863757118750514530377960854078525056266047282679272890618141751020515246817285970966 10764640764873848200003985701533823843693216061927050717147320494948450610723651059190791002422692624017614826832336800842789 32776605301190743625750069668595999083839919149274080973925726463875601039024388822290730150475429089500986559652364866543968 38810664811466043041895318575756388868570705975638776910627485874343724466486679668478304694983275795348266889776558364095444 404"
    ]
]
for (let i in actualParam) Test.assertEquals(sum(...actualParam[i]), expectedParam[i])