Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

Loop through the ID's instead of doing a switch. This should do the same as yours

function colorM(id){
 // if not a number
 if (!(!isNaN(parseFloat(n)) && isFinite(n))) id =1;
 // default for numbers
 if (id > 5 || id < 1) id = 1;
 for (var i = 1; i <= 5; i++){
 
 if (id == i){
 document.getElementById("slideM" + i).style.backgroundColor="#009CFF";
 document.getElementById("slideM" + i).className="show";
 document.getElementById("slideC" + i).style.display="block";
 }
 else {
 document.getElementById("slideM" + i).style.backgroundColor="silver";
 document.getElementById("slideM" + i).className="";
 document.getElementById("slideC" + i).style.display="none";
 }
 }
}

Edit: added a check if id is numeric id is numeric AND id is within range

Loop through the ID's instead of doing a switch. This should do the same as yours

function colorM(id){
 // if not a number
 if (!(!isNaN(parseFloat(n)) && isFinite(n))) id =1;
 // default for numbers
 if (id > 5 || id < 1) id = 1;
 for (var i = 1; i <= 5; i++){
 
 if (id == i){
 document.getElementById("slideM" + i).style.backgroundColor="#009CFF";
 document.getElementById("slideM" + i).className="show";
 document.getElementById("slideC" + i).style.display="block";
 }
 else {
 document.getElementById("slideM" + i).style.backgroundColor="silver";
 document.getElementById("slideM" + i).className="";
 document.getElementById("slideC" + i).style.display="none";
 }
 }
}

Edit: added a check if id is numeric AND id is within range

Loop through the ID's instead of doing a switch. This should do the same as yours

function colorM(id){
 // if not a number
 if (!(!isNaN(parseFloat(n)) && isFinite(n))) id =1;
 // default for numbers
 if (id > 5 || id < 1) id = 1;
 for (var i = 1; i <= 5; i++){
 
 if (id == i){
 document.getElementById("slideM" + i).style.backgroundColor="#009CFF";
 document.getElementById("slideM" + i).className="show";
 document.getElementById("slideC" + i).style.display="block";
 }
 else {
 document.getElementById("slideM" + i).style.backgroundColor="silver";
 document.getElementById("slideM" + i).className="";
 document.getElementById("slideC" + i).style.display="none";
 }
 }
}

Edit: added a check if id is numeric AND id is within range

edited body
Source Link
Topener
  • 519
  • 4
  • 8

Loop through the ID's instead of doing a switch. This should do the same as yours

function colorM(id){
 // if not a number
 if (!(!isNaN(parseFloat(n)) && isFinite(n))) id =1;
 // default for numbers
 if (id > 5 || id < 01) id = 1;
 for (var i = 1; i <= 5; i++){
 
 if (id == i){
 document.getElementById("slideM" + i).style.backgroundColor="#009CFF";
 document.getElementById("slideM" + i).className="show";
 document.getElementById("slideC" + i).style.display="block";
 }
 else {
 document.getElementById("slideM" + i).style.backgroundColor="silver";
 document.getElementById("slideM" + i).className="";
 document.getElementById("slideC" + i).style.display="none";
 }
 }
}

Edit: added a check if id is numeric AND id is within range

Loop through the ID's instead of doing a switch. This should do the same as yours

function colorM(id){
 // if not a number
 if (!(!isNaN(parseFloat(n)) && isFinite(n))) id =1;
 // default for numbers
 if (id > 5 || id < 0) id = 1;
 for (var i = 1; i <= 5; i++){
 
 if (id == i){
 document.getElementById("slideM" + i).style.backgroundColor="#009CFF";
 document.getElementById("slideM" + i).className="show";
 document.getElementById("slideC" + i).style.display="block";
 }
 else {
 document.getElementById("slideM" + i).style.backgroundColor="silver";
 document.getElementById("slideM" + i).className="";
 document.getElementById("slideC" + i).style.display="none";
 }
 }
}

Edit: added a check if id is numeric AND id is within range

Loop through the ID's instead of doing a switch. This should do the same as yours

function colorM(id){
 // if not a number
 if (!(!isNaN(parseFloat(n)) && isFinite(n))) id =1;
 // default for numbers
 if (id > 5 || id < 1) id = 1;
 for (var i = 1; i <= 5; i++){
 
 if (id == i){
 document.getElementById("slideM" + i).style.backgroundColor="#009CFF";
 document.getElementById("slideM" + i).className="show";
 document.getElementById("slideC" + i).style.display="block";
 }
 else {
 document.getElementById("slideM" + i).style.backgroundColor="silver";
 document.getElementById("slideM" + i).className="";
 document.getElementById("slideC" + i).style.display="none";
 }
 }
}

Edit: added a check if id is numeric AND id is within range

added 158 characters in body
Source Link
Topener
  • 519
  • 4
  • 8

Loop through the ID's instead of doing a switch. This should do the same as yours

function colorM(id){
 // if not a number
 if (!(!isNaN(parseFloat(n)) && isFinite(n))) id =1;
 // default for numbers
 if (id > 5 || id < 0) id = 1;
 for (var i = 1; i <= 5; i++){
 
 if (id == i){
 document.getElementById("slideM" + i).style.backgroundColor="#009CFF";
 document.getElementById("slideM" + i).className="show";
 document.getElementById("slideC" + i).style.display="block";
 }
 else {
 document.getElementById("slideM" + i).style.backgroundColor="silver";
 document.getElementById("slideM" + i).className="";
 document.getElementById("slideC" + i).style.display="none";
 }
 }
}

Edit: added a check if id is numeric AND id is within range

Loop through the ID's instead of doing a switch. This should do the same as yours

function colorM(id){
 for (var i = 1; i <= 5; i++){
 
 if (id == i){
 document.getElementById("slideM" + i).style.backgroundColor="#009CFF";
 document.getElementById("slideM" + i).className="show";
 document.getElementById("slideC" + i).style.display="block";
 }
 else {
 document.getElementById("slideM" + i).style.backgroundColor="silver";
 document.getElementById("slideM" + i).className="";
 document.getElementById("slideC" + i).style.display="none";
 }
 }
}

Loop through the ID's instead of doing a switch. This should do the same as yours

function colorM(id){
 // if not a number
 if (!(!isNaN(parseFloat(n)) && isFinite(n))) id =1;
 // default for numbers
 if (id > 5 || id < 0) id = 1;
 for (var i = 1; i <= 5; i++){
 
 if (id == i){
 document.getElementById("slideM" + i).style.backgroundColor="#009CFF";
 document.getElementById("slideM" + i).className="show";
 document.getElementById("slideC" + i).style.display="block";
 }
 else {
 document.getElementById("slideM" + i).style.backgroundColor="silver";
 document.getElementById("slideM" + i).className="";
 document.getElementById("slideC" + i).style.display="none";
 }
 }
}

Edit: added a check if id is numeric AND id is within range

deleted 23 characters in body
Source Link
Topener
  • 519
  • 4
  • 8
Loading
Source Link
Topener
  • 519
  • 4
  • 8
Loading
default

AltStyle によって変換されたページ (->オリジナル) /