Last active
December 24, 2015 17:09
-
-
Save jakepruitt/6833413 to your computer and use it in GitHub Desktop.
Alternating rows function Code Challenge
This file contains hidden or 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
| // A function that takes the total number of rows, the total number of columns, oddOrder and evenOrder arrays and an object inputDataObject that contains the arrays to be used as input data. | |
| // The structure of the order arrays must be ["inputData1","inputData2","inputData3"] | |
| function renderAlternatingGrid (totalRowNumber, totalColumnNumber, oddOrder, evenOrder, inputDataObject) { | |
| var alternatingGrid = []; | |
| for (var rowIndex = 0; rowIndex < totalRowNumber; rowIndex++) { | |
| alternatingGrid[rowIndex] = []; | |
| function orderRowElements (order) { | |
| for (var columnIndex = 0; columnIndex < totalColumnNumber; columnIndex++) { | |
| var dataKey = order[columnIndex]; | |
| alternatingGrid[rowIndex][columnIndex] = inputDataObject[dataKey][rowIndex]; | |
| }; | |
| } | |
| if (rowIndex%2 == 0) { // If it is an odd row | |
| orderRowElements(oddOrder); | |
| } else { // If it is an even row | |
| orderRowElements(evenOrder); | |
| }; | |
| }; | |
| return alternatingGrid; | |
| } | |
| var newAlternatingGrid = renderAlternatingGrid(3,3,["inputData1","inputData2","inputData3"],["inputData2","inputData3","inputData1"],{ | |
| "inputData1":["question1","question2","question3"], | |
| "inputData2":["video1","video2","video3"], | |
| "inputData3":["article1","article2","article3"] | |
| }); | |
| console.log(newAlternatingGrid); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment