// EXAMPLE input (encrypted strategy guide) converted to a multiline stringconststrategyGuide=`A Y
B X
C Z`// opponent is first column and this is opponent winningconstoppWinning={A:'Z',B:'X',C:'Y'}// I am second column and this is me winningconstmeWinning={A:'Y',B:'Z',C:'X'}// points granted for use of toolconsttoolPoints={X:1,Y:2,Z:3}constgetMyTotalScore=(input)=>{constinputAsArr=input.split('\n').map(str=>str.split(''))// the reason 'input' is converted to an array of arrays instead of an object of key-value pairs is because the ACTUAL input (i.e., not the EXAMPLE input we're working with here) would, if set up as an object, have duplicate keys, and of course duplicate keys are not allowed in JavaScript objects (iterating through the keys of a JavaScript object that has duplicate keys doesn't work)letmyTotalScore=0for (leti=0;i<inputAsArr.length;i++){myTotalScore+=toolPoints[inputAsArr[i][1]]if (inputAsArr[i][1]===meWinning[inputAsArr[i][0]]){myTotalScore+=6}elseif (inputAsArr[i][1]===oppWinning[inputAsArr[i][0]]){myTotalScore}else{myTotalScore+=3}}returnmyTotalScore}console.log(getMyTotalScore(strategyGuide))// 15
Top comments (0)
Subscribe
We're a place where coders share, stay up-to-date and grow their careers.
Top comments (0)