0

Currently this script takes the words from the list and generates a grid, giving the words random positions each time. When the words are generated I want them to be split into individual characters, into cells next to each other - how do I do this?

  var listOfWords = ["mat", "cat", "dog", "pit", "pot", "fog", "log", "pan", "can",  "man", ];
  var shuffledWords = listOfWords.slice(0, 12);
  shuffledWords.sort(function() {
  return 0.5 - Math.random();
});

 var table = $('<table class="tablestyle">');
 var rows = 6;
 var cols = 2;
 var tr;
 var index;

for (var row = 0; row < rows; row++) {
tr = $('<tr>');
for (var col = 0; col < cols; col++) {
    index = (row * cols) + col;
    $('<td>').text(shuffledWords[index]).appendTo(tr);
}
tr.appendTo(table);
}

 table.appendTo('body');

 $('<table>' + innerTable + '</table>').appendTo('body');

2 Answers 2

1

Try sorting the list randomly before slicing it. Like this

listOfWords.sort(function() {
  return 0.5 - Math.random();
});
var shuffledWords = listOfWords.slice(0, 12);
Sign up to request clarification or add additional context in comments.

2 Comments

Using sort like this isn't a good idea. It expects that the comparison function returns the same result for the same parameters, which can lead to unintended and difficult to find bugs.
The sort function is just copied from above. All that was changed was the order.
0
<script type="text/javascript">
var html = "<table border='0' width='100%'>";
html += "<tr> <td> Search ID </td> <td> Name </td> <td>Location</> <td> Reason </td></tr>"
html += "<tr> <td> 1 </td> <td> Bob </td> <td>Glasgow</> <td> Hello </td></tr>"
html+="</table>";

$('#outputID').append(html);
</script>

Where it says

<td> Bob </td> 

replace it with

"<td>" + varibleName + "</td>"

And the varible name could be each character? Just put this in a loop that suits?

Hope this helps

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.