Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
JavaScript
if (i <= 5) {
    var listItem = "item" + i;
    document.getElementById(listItem).innerHTML=document.getElementById("toolBox").value;
    document.getElementById("toolBox").value = "";
	if (i === 5) {
	  document.getElementById("resultsExpl").innerHTML="Thanks for your suggestions.";
  }
  i++;
 }
}


What I have tried:

I'm an absolute beginner. This is wrong. Instructions call for 2 cases; one for 5 and one default.
JavaScript
switch (i) {
 case 5: 
    var listItem = "item" + i;
    document.getElementById(listItem).innerHTML=document.getElementById("toolBox").value;
    document.getElementById("toolBox").value = "";
  break;
  
 default: 
    document.getElementById("resultsExpl").innerHTML="Thanks for your suggestions.";
  break;
}
Posted
Updated 19-Feb-16 8:39am
v2
Comments
[no name] 19-Feb-16 14:30pm    
There is no need of switch for above code. Secondly it looks like some code is common means it will execute every time so there is no need to put it inside switch case. That code is :

var listItem = "item" + i;
document.getElementById(listItem).innerHTML=document.getElementById("toolBox").value;
document.getElementById("toolBox").value = "";
Sergey Alexandrovich Kryukov 19-Feb-16 14:40pm    
You are right, but there is another big issue, related to '==='. Please see Solution 1. I credited your comment.
—SA

1 solution

Manas Kumar is right. We simply don't need to consider your switch, wrong or not. Besides, hard-coding "5" might be bad: you are not reusing the constant and, hence, can have trouble supporting the code.

But there is one more problem, a deeper one. The condition i === 5 suggests that you are not sure that i is numeric. But you are using the condition i <= 5 above, which suggests that i should already be tested as numeric, which is not the case. You have to do the '===' check before numerical comparison. And this is another reason for not considering your switch piece of code. Review of modified code could make sense only if your original code is correct, and if it's known what it achieves. Due to the problem I described, this is not the case. If this '===' condition makes sense at all, switch should not be used.

The identity (strict equality) operator '===', as opposed to equality operator '==', is explained, for example, here: Comparison operators — JavaScript | MDN[^].

The practice of using JavaScript by "absolute beginners" (as you said) is interesting and, well… questionable. This language is pleasure to use; being very simple and elegant, it is, conceptually, quite tricky and unusual, is the world's most misunderstood language:
JavaScript: The World's Most Misunderstood Programming Language[^],
The World's Most Misunderstood Programming Language by Douglas Crockford — YOW! 2013 | Eventer[^].

—SA
 
Share this answer
 
v8

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900