Created
December 1, 2017 17:58
-
-
Save hectorlorenzo/bdb9e57061908bf255645aec749ba8ec to your computer and use it in GitHub Desktop.
Advent of code day 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sumConsecutive (number) { | |
var arrayOfNumbers = number.split('') | |
var sum = 0 | |
var currentNumber = null | |
var nextNumber = null | |
var arrayLength = arrayOfNumbers.length | |
for (var i = arrayLength - 1; i >= 0; i--) { | |
currentNumber = arrayOfNumbers[i] | |
if (typeof arrayOfNumbers[i + 1] === 'undefined') { | |
nextNumber = arrayOfNumbers[0] | |
} else { | |
nextNumber = arrayOfNumbers[i + 1] | |
} | |
if (currentNumber === nextNumber) { | |
sum += parseInt(currentNumber) | |
} | |
} | |
return sum | |
} | |
console.log(sumConsecutive('77736991856689225253142335214746294932318813454849177823468674346512426482777696993348135287531487622845155339235443718798255411492778415157351753377959586612882455464736285648473397681163729345143319577258292849619491486748832944425643737899293811819448271546283914592546989275992844383947572926628695617661344293284789225493932487897149244685921644561896799491668147588536732985476538413354195246785378443492137893161362862587297219368699689318441563683292683855151652394244688119527728613756153348584975372656877565662527436152551476175644428333449297581939357656843784849965764796365272113837436618857363585783813291999774718355479485961244782148994281845717611589612672436243788252212252489833952785291284935439662751339273847424621193587955284885915987692812313251556836958571335334281322495251889724281863765636441971178795365413267178792118544937392522893132283573129821178591214594778712292228515169348771198167462495988252456944269678515277886142827218825358561772588377998394984947946121983115158951297156321289231481348126998584455974277123213413359859659339792627742476688827577318285573236187838749444212666293172899385531383551142896847178342163129883523694183388123567744916752899386265368245342587281521723872555392212596227684414269667696229995976182762587281829533181925696289733325513618571116199419759821597197636415243789757789129824537812428338192536462468554399548893532588928486825398895911533744671691387494516395641555683144968644717265849634943691721391779987198764147667349266877149238695714118982841721323853294642175381514347345237721288281254828745122878268792661867994785585131534136646954347165597315643658739688567246339618795777125767432162928257331951255792438831957359141651634491912746875748363394329848227391812251812842263277229514125426682179711184717737714178235995431465217547759282779499842892993556918977773236196185348965713241211365895519697294982523166196268941976859987925578945185217127344619169353395993198368185217391883839449331638641744279836858188235296951745922667612379649453277174224722894599153367373494255388826855322712652812127873536473277')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment