1

Had this code running fine when I was using a javascript window prompt to input the length. I have now changed it to use an if statement and a dropdown window in the html, and it does not seem to work? Been trying to figure out where the problem is for a few hours, so would be really grateful if someone could point me in the right direction!

HTML:

<select name="paraSize" id ="paraSize">
 <option value="small">Small</option>
 <option value="medium">Medium</option>
 <option value="large">Large</option>
</select>

JavaScript:

var getRekt = function() {
 var ipsumLength = 0
 var numberParagraph = document.getElementById("paragraphs").value;
 var paragraphSize = document.getElementById("paraSize").value;
 var startRektIpsum = document.getElementById("startRekt").checked;
 if (paragraphSize === small) {
 (ipsumLength = 9);
 };
 else if (paragraphSize === medium) {
 (ipsumLength = 25);
 };
 else if (paragraphSize === large) {
 (ipsumLength= 38);
 };
 var string = "Rekt Ipsum ";
 var rektIpsum = [
 //Array of words which I won't bore you with
 ];
 for (i = 0; i <= ipsumLength; i++) {
 (string = (string + " " + rektIpsum[Math.floor((Math.random() * (rektIpsum.length)) )]))
 }
 document.getElementById("p1").innerHTML = (string);
}
Weafs.py
23k9 gold badges58 silver badges80 bronze badges
asked Oct 28, 2014 at 9:30
5
  • ehrm ... what's up doc? you're calling several ids that you're not posting here and you're not explaining what is the problem ... Commented Oct 28, 2014 at 9:34
  • first problem, in paragraphSize === small the value small should be a string literal from what I can understand... so it should be paragraphSize === 'small' - same for the rest of the if conditions also Commented Oct 28, 2014 at 9:37
  • the ; at the end of if and else blocks should not be there Commented Oct 28, 2014 at 9:38
  • Sorry if the post wasn't clear, removing those semi colons and changing the values to a string seems to have done the trick, thanks so much Commented Oct 28, 2014 at 9:42
  • You're still lacking a ; at the end of the first variable definition and you have () where they're not needed ... Commented Oct 28, 2014 at 9:44

1 Answer 1

2

As noted in my comments the problems was syntactical... but the solution can be simplified to

var paragraphSizeMap = {
 small: 9,
 medium: 25,
 large: 38
}
var getRekt = function () {
 var numberParagraph = document.getElementById("paragraphs").value;
 var paragraphSize = document.getElementById("paraSize").value;
 var startRektIpsum = document.getElementById("startRekt").checked;
 var ipsumLength = paragraphSizeMap[paragraphSize] || 0;
 var string = "Rekt Ipsum ";
 var rektIpsum = ['', '', ''];
 for (i = 0; i < ipsumLength; i++) {
 string += " " + rektIpsum[Math.floor(Math.random() * rektIpsum.length)]
 }
 document.getElementById("p1").innerHTML = string;
}
answered Oct 28, 2014 at 9:51
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.