I'm running this function to flatten an array:
function flatten(array) {
return array.join(',').split(',');
}
array = [[1,2,3],[1,2,3]];
alert(flatten(array));
it's working in the below snippet but when I try to use it in the site codewars, I get '\' inserted at each character, causing the test to fail. Why is that?
This is the output:
Time: 496ms Passed: 0 Failed: 78
Test Results:
Basic tests
✘ Expected: '[]', instead got: '[\'\']'
✘ Expected: '[]', instead got: '[\'\', \'\']'
✘ Expected: '[1]', instead got: '[\'\', \'1\']'
✘ Expected: '[1, 2]', instead got: '[\'\', \'\', \'\', \'2\', \'\', \'1\']'
✘ Expected: '[1, 2, 3, 4, 5, 6, 7, 8, 9]', instead got: '[\'3\', \'2\', \'1\', \'7\', \'9\', \'8\', \'6\', \'4\', \'5\']'
✘ Expected: '[1, 2, 3, 4, 5, 6, 100]', instead got: '[\'1\', \'3\', \'5\', \'100\', \'2\', \'4\', \'6\']'
✘ Expected: '[111, 222, 333, 444, 555, 666, 777, 888, 999]', instead got: '[\'111\', \'999\', \'222\', \'333\', \'444\', \'888\', \'777\', \'666\', \'555\']'
Completed in 7ms
These are the tests:
describe("Example test cases", function() {
Test.assertSimilar(flattenAndSort([]), []);
Test.assertSimilar(flattenAndSort([[], [1]]), [1]);
Test.assertSimilar(flattenAndSort([[3, 2, 1], [7, 9, 8], [6, 4, 5]]), [1, 2, 3, 4, 5, 6, 7, 8, 9]);
Test.assertSimilar(flattenAndSort([[1, 3, 5], [100], [2, 4, 6]]), [1, 2, 3, 4, 5, 6, 100]);
});