Hi its 2022 now lol, guys i see everyone uses .replace to change the history values, but i did it in other way, the var newStory transformed the string into an array by dividing the items using the ‘:’, so i was able to replace the inserX,Y,Z just by its position, here is the code:
const customName = document.getElementById(‘customname’);
const randomize = document.querySelector(’.randomize’);
const story = document.querySelector(’.story’);
function randomValueFromArray(array){
const random = Math.floor(Math.random()*array.length);
return array[random];
}
var storyText = ‘It was 94 fahrenheit outside, so :insertx: went for a walk. When they got to :inserty:, they stared in horror for a few moments, then :insertz:. Bob saw the whole thing, but was not surprised — :insertx: weighs 300 pounds, and it was a hot day.’;
insertX = [‘Willy the Goblin’,‘Big Daddy’,‘Father Christmas’];
insertY = [‘the soup kitchen’,‘Disneyland’,‘the White House’];
insertZ = [‘spontaneously combusted’,‘melted into a puddle on the sidewalk’,‘turned into a slug and crawled away’],
randomize.addEventListener(‘click’, result);
function result() {
let newStory = storyText.split(':');
xItem = randomValueFromArray(insertX);
yItem = randomValueFromArray(insertY);
zItem = randomValueFromArray(insertZ);
newStory[1] = xItem;
newStory[3] = yItem;
newStory[5] = zItem;
newStory[7] = xItem;
if(customName.value !== ‘’) {
const name = customName.value;
newStory[6] =' ' + name + ' saw the whole thing, but was not surprised';
}
if(document.getElementById(“uk”).checked) {
const weight = Math.round(300);
const temperature = Math.round(94);
const temperatureC = Math.round((temperature - 32) * 5/9);
console.log(temperatureC);
newStory[0] = 'It was ' + temperatureC + ' celcius outside, so '
const weightUk = Math.round(weight / 14);
newStory[8] = 'weighs ' + weightUk + ' stones, and it was a hot day';
}
story.textContent = newStory;
story.style.visibility = ‘visible’;
console.log(newStory);
}