Note: my code may not be the best and am open to contradiction and correction
hey i can help you with changing the weight and temperature and bob . i have tried to edit your code minimal. please note constant are only accessible in the
function defined in so the weight ,temperature and customname needed to be defined earlier at the beginning of the function(thats why the were not accessible since you only defined them in an if statement).
am pasting the code below : first add a new paragraph class ("storytext) to html and edit the other querySelectors to same classname as mine , i have commented out some of your code for your understanding. here is the .js file
const customName = document.getElementById(‘customname’);
const randomize = document.querySelector(’.randomize’);
const story = document.querySelector(’.story’);
const storyText = document.querySelector(’.storytext’);
const newStory = document.querySelector(’.newStory’);
function randomValueFromArray(array){
const random = Math.floor(Math.random()*array.length);
return array[random];
}
story.textContent = “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.”
const insertX = [“Willy the Goblin”, “Big Daddy”, “Father Christmas”];
const insertY = [“the soup kitchen”, “Disneyland”, “the White House”];
const insertZ = [“spontaneously combusted”, “melted into a puddle on the sidewalk”, “turned into a slug and crawled away”];
const xItem = randomValueFromArray(insertX);
const yItem = randomValueFromArray(insertY);
const zItem = randomValueFromArray(insertZ);
storyText.textContent = story.textContent.replaceAll(":insertx:", xItem).replaceAll(":inserty:", yItem).replaceAll(":insertz:", zItem);
randomize.addEventListener(‘click’, result);
function result() {
// .replaceAll("Bob", customName.value)
// .replaceAll('300 pounds', /* weight wont work. I need to add a specific value */)
// .replaceAll('94 fahrenheit', /* temperature wont work. I need to add a specific value */)
const name = customName.value;
if(customName.value !== '') {
// newStory.replaceAll("Bob", customName.value)
let weight, temperature;
if(document.getElementById("uk").checked) {
weight = Math.round(300) / 14 + " stones";
temperature = Math.round(94 - 32) * 5/9 + " centigrade";
newStory.textContent = storyText.textContent.replaceAll('Bob',name);
}
else{
weight = Math.round(250) + 'kgs';
temperature = Math.round(67) + ' centigrade';
newStory.textContent = storyText.textContent.replaceAll('300 pounds', weight).replaceAll('94 fahrenheit', temperature).replace("Bob", name);
//story.textContent = newStory;
// story.textContent = replaced;
}
newStory.style.visibility = ‘visible’;
}
}’
https://jsfiddle.net/a21bhzcy/1/