Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JaymesKat/7f8eb25bfe43215109b67c9c95bd2fa2 to your computer and use it in GitHub Desktop.
Save JaymesKat/7f8eb25bfe43215109b67c9c95bd2fa2 to your computer and use it in GitHub Desktop.
Solution to First Assessment Exercise in Mozilla JavaScript course
var customName = document.getElementById('customname');
var randomize = document.querySelector('.randomize');
var story = document.querySelector('.story');
function randomValueFromArray(array){
return array[Math.floor(Math.random()*array.length)];
}
var storyText = "It was 94 farenheit 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 he 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"];
randomize.addEventListener('click', result);
function result() {
var newStory = storyText;
var xItem, yItem, zItem;
xItem = randomValueFromArray(insertX);
yItem = randomValueFromArray(insertY);
zItem = randomValueFromArray(insertZ);
newStory = newStory.replace(":insertx:",xItem);
newStory = newStory.replace(":inserty:",yItem);
newStory = newStory.replace(":insertz:",zItem);
newStory = newStory.replace(":insertx:",xItem);
if(customName.value != '') {
var name = customName.value;
newStory = newStory.replace('Bob', name);
}
if(document.getElementById("uk").checked) {
var stonesPerPound = 0.0714286;
var weight = Math.round(300*stonesPerPound)+' stone';
var temperature = Math.round((94-32)*5/9)+' centigrade';
newStory = newStory.replace("94 farenheit", temperature);
newStory = newStory.replace("300 pounds", weight);
}
story.textContent = newStory;
story.style.visibility = 'visible';
}
@AbooSalmaan
Copy link

Thanks , finally. I had been tryimg to use the .slice method to grab the imdex of the :insert:s , but it's all messed-up. I think I'm good with this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment