Toby Cannon
Toby Cannon

Reputation: 735

Javascript won't change page content

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);
}

Upvotes: 1

Views: 299

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388316

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;
}

Upvotes: 2

Related Questions