Skip to content

Instantly share code, notes, and snippets.

@AlexanderKozhevin
Created June 8, 2016 20:39
Show Gist options
  • Save AlexanderKozhevin/c48a6a7cad726d1aab17d7c11a1e8cbf to your computer and use it in GitHub Desktop.
Save AlexanderKozhevin/c48a6a7cad726d1aab17d7c11a1e8cbf to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<body>
<p>Click the button to loop through a block of code as long as i is less than 10.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var str = 'new hacking home network network mobility network less home genius smart drugs piano play iot javascript minsk afterparty functional programming reactive programming network of home cards memory overflow bike'
var i = str.length;
var chunks = [];
var chunkSize = 30;
while (i > 0) {
var temp = str.substr(0, chunkSize);
var lastSpaceIndex = temp.lastIndexOf(' ');
if (i<chunkSize){
lastSpaceIndex = i;
}
var newChunk = temp.substr(0, lastSpaceIndex);
console.log(newChunk)
chunks.push(newChunk)
str = str.substr(newChunk.length)
console.log(str)
i -= newChunk.length;
console.log(i)
}
console.log(chunks)
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment