Utilizing an array of words, we are in the process of generating random text. The goal is to shuffle and combine these words to form new "random sentences". However, we have encountered a minor issue - the addition of "undefined" at the beginning of each paragraph, as well as incorporating random words at the end. Any assistance with this matter would be greatly appreciated.
We aim to ensure that each sentence commences with a capital letter and concludes with a period.
Note: While the snippet does not produce randomized text, it functions correctly in the .js file.
var buttonElem = document.getElementById("DoAgain");
buttonElem.addEventListener('click', makeNewString);
var str = "Lorem ipsum dolor sit amet consectetur adipiscing elit Nullam malesuada ac nulla id interdum Nam volutpat nulla id neque scelerisque ut imperdiet tellus mattis Aliquam a consectetur felis Praesent at eros lorem Nunc id pretium lacus sed porta ex Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas Maecenas mollis posuere auctor Aenean ultrices eleifend diam mattis varius tellus convallis sagittis Donec ut lacus vel nulla laoreet mollis Quisque faucibus nunc at nisi ullamcorper facilisis";
var mixedRes = [];
var currentPosition = 0;
var newString = "";
function addTag(){
newString = "<p>" + str + "</p>";
}
function makeNewString() {
str=str.toLocaleLowerCase();
var res = str.split(" ");
mixedRes = shuffle(res);
for(var i=0; i<mixedRes.length; i++)
{
newString += mixedRes[i] + " ";
}
newString+=makeSentence(6);
document.getElementById("demo0").innerHTML = newString;
}
function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex;
while (0 !== currentIndex) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
function capitalise(word){
return (word.charAt(0).toUpperCase()+word.slice(1));
}
function makeSentence(slenght){
var slength=randomNumber(5,10);
var upperCaseLetter = "";
upperCaseLetter=mixedRes[currentPosition].charAt(0).toUpperCase();
for (var i=currentPosition+1; i<currentPosition+slength;i++){
newString+=mixedRes[i]+"";
}
currentPosition+=slength;
var finalString=upperCaseLetter+newString+".";
document.getElementById("demo1").innerHTML = finalString;
}
function makeParagraph(){
newString+="\n";
}
function randomNumber(a,b) {
return Math.round(Math.random()*(b-a)+parseInt(a));
}
<p id="demo0">Lorem ipsum dolor sit amet consectetur adipiscing elit Nullam malesuada ac nulla id interdum Nam volutpat nulla id neque scelerisque ut imperdiet tellus mattis Aliquam a consectetur felis Praesent at eros lorem Nunc id pretium lacus sed porta ex Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas Maecenas mollis posuere auctor Aenean ultrices eleifend diam mattis varius tellus convallis sagittis Donec ut lacus vel nulla laoreet mollis Quisque faucibus nunc at nisi ullamcorper facilisis.</p>
<button class="button1" id="DoAgain"> DO IT AGAIN</button>