Skip to content

Instantly share code, notes, and snippets.

@theRemix
Last active August 29, 2015 14:08
Show Gist options
  • Save theRemix/bf147e5f06542cdce3d0 to your computer and use it in GitHub Desktop.
Save theRemix/bf147e5f06542cdce3d0 to your computer and use it in GitHub Desktop.
Multidimensional Arrays

Multidimensional Arrays

Setup

  • create a project in your DevLeague directory named "MultiDimensional Arrays"
  • initialize a git repository in this project
  • create a .gitignore file and add node_modules as an ignored path
  • initialize an npm project named "MultiDimensional Arrays"
  • install any dependencies you need to run tests with mocha and chai
  • create a subdirectory called test/
  • create a test file in the test/ directory named test-generator.js
  • create a test file in the test/ directory named test-counter.js
  • create a module file in the project directory named MultiDimensionalArray.js
  • commit all your stub files

References

Part 1

The generator

  • setup test-generator.js with tests to be run by mocha.

  • before all tests, import the MultiDimensionalArray module

  • describe the behavior of calling the MultiDimensionalArray.generate method with no arguments, it should return an array (length 3) each containing arrays (length 3) with random boolean values. ex:

    [  
      [0,1,0],  
      [1,1,0],  
      [0,1,1]  
    ]
    
  • test that the resulting array has length 3, and has 3 elements that each have length 3, and these elements may be of any value.

  • commit all your work

  • describe the behavior of calling the MultiDimensionalArray.generate method with one argument, 4. generate should return a single array (length 4) each containing a random boolean value. ex:

    [1,0,1,1]
    
  • commit all your work

  • describe the behavior of calling the MultiDimensionalArray.generate method with two arguments: 5 and 4. It should return an array (length 5) each containing arrays (length 4) with random boolean values. ex:

    [
      [1,0,1,1],  
      [0,1,0,0],  
      [1,1,0,1],  
      [0,0,0,1],  
      [0,1,1,1]  
    ]
    
  • commit all your work

  • describe the behavior of calling the MultiDimensionalArray.generate method with three arguments: 3, 4, 5. It should return an array (length 3) each containing arrays (length 4) each containing arrays (length 5) with random boolean values. ex:

    [  
      [  
        [1,0,1,0,0],  
        [0,1,0,0,1],  
        [1,1,0,1,1],  
        [0,0,0,0,1]  
      ],  
      [  
        [1,1,0,1,1],  
        [1,0,1,0,0],  
        [0,1,0,0,1],  
        [0,0,0,0,1]  
      ],  
      [  
        [1,0,1,0,0],  
        [1,1,0,1,1],  
        [0,0,0,0,1],  
        [0,1,0,0,1]  
      ]  
    ]
    
  • commit all your work

Part 2

The counter

  • setup test-counter.js with tests to be run by mocha.

  • before all tests, import the MultiDimensionalArray module

  • describe the behavior of calling the MultiDimensionalArray.count method with two arguments, sample_array, true . It should return a number representing how many true values there are inside each array in test_array. values. ex:

    var sample_array = [1,0,0,1,0,0,0,0,1];  
    MultiDimensionalArray.count(sample_array, true);  
    // should return 3  
    
  • describe the behavior of calling the MultiDimensionalArray.count method with two arguments, sample_array, false . It should return a number representing how many false values there are inside each array in test_array. values. ex:

    var sample_array = [[1,0],[0,1],[0,0],[0,0]];
    MultiDimensionalArray.count(sample_array, false);
    // should return 6
    
  • describe the behavior of calling the MultiDimensionalArray.count method with two arguments, sample_array, "red" . It should return a number representing how many "red" values there are inside each array in test_array. values. ex:

    var sample_array = [  
      [  
        ["red","green","blue"],  
        ["red","green","blue"],  
        ["red","green","blue"],  
      ],  
      [  
        ["red","green","red"],  
        ["red","green","red"],  
        ["red","green","red"],  
      ],  
      [  
        ["red","red","blue"],  
        ["red","red","blue"],  
        ["red","red","blue"],  
      ],  
    ];  
    MultiDimensionalArray.count(sample_array, "red");  
    // should return 15  
    
  • commit all your work

Part 3

  • run the tests, review the results

Part 4

The Implementation

  • setup MultiDimensionalArray.js as a node module.
  • implement a method named generate so that the tests in test-generator.js pass
    • this method will accept any amount of arguments
    • this method will create a new array, and finally return this array
    • the first argument will represent the number of elements the array will contain
      • if there are no arguments passed in to generate then this array will contain 3 arrays that each contain 3 random boolean elements between (0|1)
      • if there is only one argument passed in to generate then this array will contain n many random boolean elements between (0|1)
    • the optional second argument represents how many random elements each array in the second tier will contain
      • instead of the first array having n many random boolean values, it will contain n many second tier arrays (which have a random boolean values)
    • the generate method can accept any number of parameters, each time continuing this process
      • for example, passing in 3 arguments (1,2,3) will generate a 3 dimensional array, where first array contains 1 array that contains 2 arrays that each contain 3 random boolean values
  • commit all your work
  • implement a method named count so that the tests in test-counter.js pass
    • this method will accept 1 or 2 arguments
    • the first parameter accepts an array
    • the optional second parameter represents the value to count in the array
    • this method will return the number of value found in (any depth) of the array
  • commit all your work
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment