Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Daniel Spearman
Daniel Spearman
6,640 Points

Switch statement issues

var i = 1; var listItem = "";

      switch (i) {
        // evaluates if the input items has reached/equals 5, returns message
        case i === 5:
          document.getElementById("resultsExpl").innerHTML = "Thanks for your suggestions.";
          break;
        // takes and processes user input
        default:
           var listItem = "item" + i;
           document.getElementById(listItem).innerHTML = 
           document.getElementById("toolBox").value;
           document.getElementById("toolBox").value = "";
           i += 1;
        break;
      }
  }

Need any assistance I can get. I am able to get the user input to print to the page just fine. The issue I am having is getting the first case's message ("Thanks for your suggestions") to print, once I have submitted 5 values.

2 Answers

Steven Parker
Steven Parker
229,732 Points

The "case" statement takes just the term to be compared with the switch variable, an equality comparison is implicitly performed.

So instead of "i === 5" you would just put "5".

Daniel Spearman
Daniel Spearman
6,640 Points

Thank you for your input.

I changed the code. I am able to get the message to populate, but now when I go to submit the 5th value... the message executes, however 5th value is never put in there with it

        switch (i) {
             // evaluates if the input items has reached/equals 5, returns message
             case 5:
                 document.getElementById("resultsExpl").innerHTML = "Thanks for your suggestions.";
                 break;
            // takes and processes user input
            default:
               var listItem = "item" + i;
               document.getElementById(listItem).innerHTML = document.getElementById("toolBox").value;
               document.getElementById("toolBox").value = "";
                i += 1;
                break;
          }
Steven Parker
Steven Parker
229,732 Points

If you want both the case 5 code and the default code to run, remove the "break" between them.

Daniel Spearman
Daniel Spearman
6,640 Points

Here is the HTML snippet that is being appended

  <article>
      <div id="results">
         <ul>
            <li id="item1"></li>
            <li id="item2"></li>
            <li id="item3"></li>
            <li id="item4"></li>
            <li id="item5"></li>
         </ul>
         <p id="resultsExpl"></p>
     </div>
     <form>
         <fieldset>
           <label for="toolBox" id="placeLabel">
             Type the name of a tool, then click Submit:
           </label>
           <input type="text" id="toolBox" />
         </fieldset>
         <fieldset>
           <button type="button" id="button">Submit</button>
         </fieldset>
     </form>
  </article>