# CoDEVIANT #2 (3/22/19) @ midnight-ish

• the list of game characters in a 2x6 grid;
• the initial position of the selection cursor (top-left is (0,0));
• a list of moves of the selection cursor (which are up, down, left, right);
• the list of characters who have been hovered by the selection cursor after all the moves (ordered and with repetition, all the ones after a move, wether successful or not, see tests);

# How Adrian is going to solve it:

`function streetFighterSelection(fighters, position, moves){   return “”;}`
`fighters = [   [“Ryu”, “E.Honda”, “Blanka”, “Guile”, “Balrog”, “Vega”],   [“Ken”, “Chun Li”, “Zangief”, “Dhalsim”, “Sagat”, “M.Bison”]];moves = [‘up’, ‘left’, ‘right’, ‘left’, ‘left’];describe(“Solution”, function(){   it(“should work with few moves”, function(){   Test.assertSimilar(streetFighterSelection(fighters, [0,0],       moves),[‘Ryu’, ‘Vega’, ‘Ryu’, ‘Vega’, ‘Balrog’]);   });});`
`var normalArray = [‘1’,’2’,’3’];`
`console.log( normalArray[0] ); // You’ll get ‘1’console.log( normalArray[1] ); // You’ll get ‘2’`
`var 2dArray = [   [‘Mario’, ’Snake’, ‘Fox’]   [‘Luigi’, ‘Otacon’, ‘Falco’]];`
`console.log(2dArray[0][1]); // I select the row first, 0, and then skip over horizontally to get to 1. Then I get “Snake”.`
`console.log(2dArray[1][2]); // I select the row first, in this case 1, and then skip over 0 and 1 (horizontally speaking) to get to 2. And we get ‘Falco’.`
• When at the extreme left or right positions of a row, pressing left (when at the left most), or pressing right (when at the right most) will result in you going to the complete other side of that row.
• In our example if I was on ‘Mario’ and I pressed left (when there is no left to go to) it would then give us ‘Fox’ (and vice versa).
• If we were hovering over ‘Luigi’ and pressed left (when there is no left to go to) it would give us ‘Falco’ (and vice versa).
• When at at a selection on the top row, pressing up will not do anything.
• When at a selection on the bottom row, pressing down will not do anything either.
`function streetFighterSelection(fighters, position, moves){   console.log(fighters);   console.log(position);   console.log(moves);   console.log(‘this is some crazy shit’);   console.log(fighters[0][0]);   console.log(fighters[0][5]);   console.log(fighters[1][2]);   return “”;}`
`fighters// [ [ ‘Ryu’, ‘E.Honda’, ‘Blanka’, ‘Guile’, ‘Balrog’, ‘Vega’ ],[ ‘Ken’, ‘Chun Li’, ‘Zangief’, ‘Dhalsim’, ‘Sagat’, ‘M.Bison’ ]]position// [ 0, 0 ]moves// [ ‘up’, ‘left’, ‘right’, ‘left’, ‘left’ ]an unprofessional comment by yours truly// this is some crazy shitfighters[0][0] //Ryufighters[0][5] //Vegafighters[1][2] //Zangief`

# }

• Make my variable called answer that will be an array:
`let answer = [];`
• Create another variable called currentPosition that will also be an array equaling the position argument fed into the argument of the function we are trying to make do what we want.
`let currentPosition = position;`
`function streetFighterSelection(fighters, position, moves){   let answer = [];   let currentPosition = position;   return “”;}`
`console.log(moves); // [ ‘up’, ‘left’, ‘right’, ‘left’, ‘left’ ]`
1. I am going to create a for-loop that iterates over each element in the moves array.
2. Inside the block of this for-loop, I am going to use a switch statement.
`switch(‘horse’) {   case ‘pig’:   console.log(‘It is the police’);   break;   case ‘crip’:   console.log(‘Blue stay true’);   break;   case ‘horse’:   console.log(‘Mr. Ed says hello’);   break;   default:   console.log(‘none of the cases matched so now you get this’);}`
`for(let i = 0; i <= moves.length — 1; i++ ){//fun stuff coming soon}`
`switch(moves[i]) {   case ‘up’:   if(currentPosition[0] !== 0) {   currentPosition[0] -= 1;   }   break;//If the moves[i] turns out to be ‘up’,//if the first value of the currentPosition variable is not 0, then we will subtract 1 from the //first value of the currentPosition variable//other wise, nothing happens   case ‘down’:   if(currentPosition[0] !== 1) {   currentPosition[0] += 1;   }//if the moves[i] turns out to be ‘down’,//If the first value of the currentPosition variable is not 1, then we will add 1 to the first//value of the currentPosition variable//otherwise nothing happens.   break;   case ‘left’:   if(currentPosition[1] == 0){     currentPosition[1] = fighters[0].length — 1;   } else {    currentPosition[1] -= 1   }//if the moves[i] turns out to be ‘left’,//If the second-value of the currentPosition variable is 0, then we will assign the length of a row of the fighters 2D array to currentPosition[1]. *We subtract 1 because things are zero indexed and we will have an error pop up if we don’t do that*//If currentPosition[1] is NOT 0, then we will just subtract 1 from what ever it is.   break;   case ‘right’:   if(currentPosition[1] == fighters[0].length -1 ){   currentPosition[1] = 0;   } else {     currentPosition[1] += 1   }//if the moves[i] turns out to be ‘right’,//If the second-value of the currentPosition variable is the length of a fighters 2D Array row (minus 1, because zero-indexing). then we will assign the second-value of the currentPosition to be 0.//Otherwise we will just add 1 to whatever it currently is.   break;   default:   console.log(“pay me”);//if for some reason it is neither up, down, left, or right then you should pay me.}`
`for(let i = 0; i <= moves.length — 1; i++ ){   switch(moves[i]) {   case ‘up’:   if(currentPosition[0] !== 0) {      currentPosition[0] -= 1;   }   break;   case ‘down’:   if(currentPosition[0] !== 1) {      currentPosition[0] += 1;   }   break;   case ‘left’:   if(currentPosition[1] == 0){      currentPosition[1] = fighters[0].length — 1;   } else {      currentPosition[1] -= 1   }   break;   case ‘right’:   if(currentPosition[1] == fighters[0].length -1 ){      currentPosition[1] = 0;   } else {      currentPosition[1] += 1   }   break;   default:      console.log(“pay me”);   }   recordSelection();}`
`function recordSelection() {   answer.push( fighters[currentPosition[0]][currentPosition[1]] );}`
`function streetFighterSelection(fighters, position, moves){   function recordSelection() {    answer.push( fighters[currentPosition[0]][currentPosition[1]] );   }let answer = [];let currentPosition = position;for(let i = 0; i <= moves.length-1; i++){   switch(moves[i]) {     case ‘up’:     if(currentPosition[0] !== 0) {     currentPosition[0] -= 1;   }   break;   case ‘down’:   if(currentPosition[0] !== 1) {      currentPosition[0] += 1;   }   break;   case ‘left’:   if(currentPosition[1] == 0){      currentPosition[1] = fighters[0].length — 1;   } else {   currentPosition[1] -= 1   }   break;   case ‘right’:   if(currentPosition[1] == fighters[0].length -1 ){    currentPosition[1] = 0;  } else {    currentPosition[1] += 1  }  break;  default:  console.log(“pay me”);  }  recordSelection(); }return answer;}`
`function streetFighterSelection(fighters, position, moves){var result = [];   moves.forEach(function(move) {       if (move === “up”) {       position[0] = 0;       } else if (move === “down”) {         position[0] = 1;       } else if (move === “right”) {         position[1] = (position[1] === 5) ? 0 : position[1] + 1;       } else if (move === “left”) {         position[1] = (position[1] === 0) ? 5 : position[1] — 1;       }    result.push(fighters[position[0]][position[1]]);   });   return result;}`
• We create variable called ‘result’ that is an empty array.
• We use the forEach method on the moves argument that is passed into the streetFighterSelection function. With ‘move’ being the placeholder for the particular element in moves being iterated on at the moment inside of a large chain of if/else statements:
• you make it only be ‘0’ which makes sense because you can’t go less than zero
• you make it only be ‘1’…again
• We set position’s second-argument is determined with the use of a conditional/ternary operator
• If position’s second argument is 5, position’s second argument becomes 0.
• If position’s second argument is not 5, then it becomes the position’s second argument plus 1.
• We set position’s second-argument is determined with the use of a conditional/ternary operator
• If position’s second argument is 0, position’s second argument becomes 5.
• If position’s second argument is not 0, then it becomes the position’s second argument minus 1
`if(‘right’){   if(position[1] == 0){   position[1] == 5;   } else {   position[1] = position[1] + 1;   }}`
`if (move === “right”) {   position[1] = (position[1] === 5) ? 0 : position[1] + 1;   if yes — — → 0 : if not ===> position[1] + 1}`
• After this if/else chain we push the value of the fighters 2D-Array at the reference point defined by our newly altered position argument’s values.
• You don’t always have to create new variables to deal with altering the values of an argument passed into a function.
• Conditional/Ternary operators are your friend.
• 2D arrays are weird, but could be pretty useful if you ever want to create a front-end experience like this or want to program your own fighting game menu.

--

--

--

is a web developer, opera singer, actor, and lover of cats. (adrian-rosales.tech)

Love podcasts or audiobooks? Learn on the go with our new app.