1

I'm use four arrays to generate difference test case, like below

var phone = ['phone1', 'phone2', 'phone3']; 
var browsers = ['chrome', 'firefox']; 
var location = ['1324','2434','4234','1234','3243']; 
var network = ['3g', '4g', 'wifi']; 
var isLogin = ['service worker', 'no service worker'];

How do I write code that will generate the test case (180 difference case). I try for loop and recursion. I just can't seem to figure out a perfect way to do it. note I'm using javascript. can only use array for loop, can't use object due to some reason.

Can anyone give me some inspiration ?

ruakh
  • 175,680
  • 26
  • 273
  • 307
Stanley Lau
  • 21
  • 1
  • 3
  • This answer: https://stackoverflow.com/questions/12303989/cartesian-product-of-multiple-arrays-in-javascript has some solutions that are relevant to your question – Hunter McMillen Jul 30 '18 at 03:32
  • use random numbers and dictionary of words read more https://stackoverflow.com/questions/966225/how-can-i-create-a-two-dimensional-array-in-javascript – Delgerbayar Dorj Jul 30 '18 at 03:38
  • "I can't use object due to some reason" --> arrays are actually objects under the hood. – Pac0 Jul 30 '18 at 14:19

1 Answers1

0

I tried some scenario in Java(Approach can be applicable in same way for Javascript) with recursion, In this case I use 3 arrays and for 2 of them I use recursion.

public class Test3 {

    static String[] names = { "Ram", "Shyam", "Mohan", "Rohan", "Saket" };
    static String[] citys = { "Bhopal", "Indore", "Ujjan", "Dewas", "Rewa" };
    static String[] addresses = { "add1", "add2", "add3", "add4", "add5" };
    static StringBuffer buffer = new StringBuffer();

    public static void main(String... strings) {

        for (String a : names) {
            System.out.println(testData(a)+"\n");
            buffer.setLength(0);
        }
    }

    public static String testData(String name) {

        return generateData(name, 0, -1);
    }

    public static String generateData(String combination, int countA, int countB) {

        if ((countA == names.length - 1) && (countB == names.length - 1)) {

            return buffer.toString();

        }

        if (countB == addresses.length - 1) {
            countA = countA + 1;
            countB = -1;
        }

        countB = countB + 1;

        buffer.append(combination + citys[countA] + addresses[countB] + "\n");

        return generateData(combination, countA, countB);

    }

}

Hope this will help!!

Simmant
  • 1,477
  • 25
  • 39