Hello, I would appreciate it if someone could take a look over my script for this assessment. Many Thanks, =)
[https://codepen.io/drgaud/pen/BaNzadO]
Here is the script, (my apologies for the comments, I find it helps me explain the code and learn it a bit better)
/**
*/
//Variables Defined here
var customName = document.getElementById(‘customname’);
var generate = document.querySelector(’.randomize’);
var story = document.querySelector(’.story’);
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.”
var insertX = [“Willy the Goblin”, “Big Daddy”, “Father Christmas”];
var insertY = [“the Soup Kitchen”, “Disneyland”, “The White House”];
var insertZ = [“spontaneously combusted”,“melted into a puddle on the sidewalk”,“turned into a slug and crawled away”]
// Functions Defined here
function randomValueFromArray(array){
//This function returns a random entry from the array that is passed into it.
return array[Math.floor(Math.random()*array.length)];
}
// Event Listener
function result(){
//Setting the function variables
let newStory = storyText; //This grabs the Main String
//Getting a random entries from each for the Arrays
let xItem = randomValueFromArray(insertX);
let yItem = randomValueFromArray(insertY);
let zItem = randomValueFromArray(insertZ);
//Logic Conditions
if (customName.value !== “”){
//If Name Input is not empty, then do this:
let name = customName.value;//assigning the input value to the name
const replaceBob = /bob/gi;//This is the regex for the replacement of the word “Bob” from the string
newStory= newStory.replace(replaceBob, name); //This replaces the name bob in the string with the new text
}
if(document.getElementById(‘uk’).checked){
//If the UK Checkbox is selected then do this:
let weight = Math.round(300);
let temp = Math.round(94);
function convertor(temperature,weightPounds){
let cent = Math.round(((temperature - 32) * 5/9)) + " Centigrade";//Converts the Temp from F to C
temp = cent; //Sets the temp to centigrade
let stone = Math.round((weightPounds/14)) + " stones" // Converts the Weight from pounds to Stone
weight = stone;//Sets the weight to stones
}
convertor(temp,weight);//This calls the function to convert the values as the function describes above
//replacing the values in the story
const replaceFahrenheit = /94 fahrenheit/gi; //This is the regex for the replacing the Fahrenheit string
const replacePounds = /300 pounds/gi; //This is the regex for replacing the pounds string
newStory = newStory.replace(replaceFahrenheit, temp).replace(replacePounds, weight); //This updates the new values to the story.
}
//This is a regex expression for searching for the following terms:
const replaceX = /:insertx:/gi;
const replaceY = /:inserty:/gi;
const replaceZ = /:insertz:/gi;
newStory = newStory.replace(replaceX, xItem).replace(replaceY,yItem).replace(replaceZ, zItem); //Updating the story with the new placeholders
story.textContent = newStory; //This updates the text content by updating the DOM using the .textContent method
story.style.visibility = ‘visible’; //This changes the css property to visible, whereas default is set to hidden.
}
generate.addEventListener(‘click’, result); //This is the event listener on the click button to run the result function.
I really do appreciate your support.
=)