lundi 29 juin 2020

If statement with OR condition not working

First, bless all you overflow angels out there. I have no JS or other language background. I have learned everything in the code for this particular problem so bear with me if things aren't clean and clever. I have done a lot of searching before resulting to asking here, so hopefully ALMOST everything is accounted for. I have a conditional statement I just CANNOT get to run correctly. (entire code for context at the bottom)

            if (pyramid < 1 || pyramid > 8) {
                var dennis = document.getElementById("dennis");
                var showdennis = "#ahahah{display: block}";
                dennis.appendChild(document.createTextNode(showdennis));
                document.getElementById("giza").innerHTML = "";
                return;
            }

I am most concerned with (pyramid < 1 || pyramid > 8) but if you can help me account for an input value of zero (due to complexities with 0 being false-y), bonus points.

<!DOCTYPE html>
<html lang="en-US">

<head>
  <meta charset="UTF-8" />
  <style id="dennis">
    #ahahah {
      display: none;
    }
  </style>
</head>

<body>
  <h1>Text Box Input and Output</h1>
  <form action="">
    <fieldset>
      <label>write how tall </label>
      <input type="number" id="numberin" min="" max="" step="1" />
      <input type="button" value="Make the Pyramid" onclick="makepyramid()" />
    </fieldset>
  </form>

  <script type="text/javascript">
    function makepyramid() {
      var numberin = document.getElementById("numberin");

      var pyramid = numberin.value;
      var spaceincrement = numberin.value;
      var octoincrement = numberin.value;
      var spaces;
      var octothorps;
      var bringittogether = "";


      //WHY YOU NO WORK?! I'd like to make 0 work as well but I am more concerned with the range first.
      //first if statement is the ideal, second is bare minimum.
      //if (pyramid === null || pyramid < 1 || pyramid > 8) {
      //if (pyramid < 1 || pyramid > 8) {
      //Put in this if statement to show what SHOULD happen
        if (pyramid > 8) {
        var dennis = document.getElementById("dennis");
        var showdennis = "#ahahah{display: block}";
        dennis.appendChild(document.createTextNode(showdennis));
        document.getElementById("giza").innerHTML = "";
        return;
      } else {
        document.getElementById("ahahah").innerHTML = "";

        //decide how many lines to make
        for (var a = 0; a < pyramid; a++) {

          //number of spaces loop
          for (var b = 1, spaces = ""; b < spaceincrement; b++) {
            spaces += "_";
          }

          //number of octothorps in one line loop
          for (var c = pyramid, octothorps = ""; c >= octoincrement; c--) {
            octothorps += "#";
          }

          //print spaces, hashes, 2 spaces, start new line
          bringittogether += spaces + octothorps + "__" + octothorps + "<br/>";
          document.getElementById("giza").innerHTML = bringittogether;
          //increment to change next line's number of spaces (one less) and octothorps (one more)
          spaceincrement = [spaceincrement - 1];
          octoincrement = [octoincrement - 1];
        }
      }
    }
  </script>
  <hr />
  <div id="giza"></div>

  <div id="ahahah">
    <p><img src="https://i.imgur.com/1A8Zgnh.gif" /> You must select a number between 1 and 8
    </p>

  </div>
</body>

</html>

Aucun commentaire:

Enregistrer un commentaire