Ad

Few links

using 'map' to iterate over an array

It can replace a for loop, basically

arrow function '=>'

Shorter syntax to write functions (ECMAscript 2015)

'const' & 'let' keyword

(ECMAscript 2015)

Writing test cases here is not that easy , since everything is random

Code
Diff
  • const generateComment = array => array.map(x=>x[Math.floor(Math.random() * x.length)]).join` `
            
    
    // console.log(generateComment([['hi,', 'hello,', 'hey,'], ['how are'], ['you', 'ya'], ['doing', 'going'], ['?', '?!', '']]));
    • var generateComment = function (array){
    • var z;
    • var k = [];
    • for (var i = 0; i < array.length; i++) {
    • z = Math.floor(Math.random() * array[i].length);
    • if (array[i][z][array.length - 1] == ',') {
    • k.push(array[i][z]);
    • } else {
    • k.push(array[i][z] + ' ');
    • }
    • };
    • return k.join('');
    • };
    • //console.log(generateComment([['hi,', 'hello,', 'hey,'], ['how are'], ['you', 'ya'], ['doing', 'going'], ['?', '?!', '']]));
    • const generateComment = array => array.map(x=>x[Math.floor(Math.random() * x.length)]).join` `
    • // console.log(generateComment([['hi,', 'hello,', 'hey,'], ['how are'], ['you', 'ya'], ['doing', 'going'], ['?', '?!', '']]));