+ 5

Can anyone see why the not statement doesn't work?

function guessTheColors(response, arr) { response = document.getElementById('color').value; arr = "red", "orange", "yellow", "green", "blue", "indigo", "violet", "RED", "ORANGE", "YELLOW", "GREEN", "BLUE", "INDIGO", "VIOLET", "Red", "Orange", "Yellow", "Green", "Blue", "Indigo", "Violet"; for(var i = 0; i <= arr.length; i++) { if (response === "") { var ans1 = document.getElementById('errMsg').innerHTML = ("Please enter a color!"); } else if (!isNaN(response)) { var ans2 = document.getElementById('errMsg').innerHTML = ("Sorry, no numeric values!"); } else if (response != arr[i]) { var ans3 = document.getElementById('errMsg').innerHTML = ("Sorry, that\'s not a color of the rainbow!"); } else if (response == arr[i]) { var ans4 = document.getElementById('errMsg').innerHTML = ("Correct! " + response + " is one of the colors of the rainbow."); } } }

9th Apr 2017, 12:14 AM
LeRoy Summers
LeRoy Summers - avatar
4 Respuestas
+ 9
/* The 'not statement' works, but your logic is wrong by iterating over your values and printing necessarly a result at each iteration ^^ You make syntax error in assignation to innerHTML attribute... don't do: var ans = document.getElementById('errMsg').innerHTML = "some text"; ... but: var ans = document.getElementById('errMsg'); ans.innerHTML = "some text"; ... or simpliest: document.getElementById('errMsg').innerHTML = "some text"; Anyway, your litteral declaration of your array is wrong too and override the unused parameter 'arr', as the 'response' one :P You can also simplify your code/data list by using toLowerCaxe() or toUpperCase() string methods: */ function guessTheColors() { // you don't need parameters response = document.getElementById('color').value.toLowerCase(); arr = ["red", "orange", "yellow", "green", "blue", "indigo", "violet"]; if (response === "") { document.getElementById('errMsg').innerHTML = "Please enter a color!"; } else if (!isNaN(response)) { document.getElementById('errMsg').innerHTML = "Sorry, no numeric values!"; } else { var is_in = false; for (var i = 0; i <= arr.length; i++) { if (response == arr[i]) { is_in = true; break; } } if (is_in) { document.getElementById('errMsg').innerHTML = "Correct! " + response + " is one of the colors of the rainbow."; } else { document.getElementById('errMsg').innerHTML = "Sorry, that\'s not a color of the rainbow!"; } } } /* Works with the minimal html code below: <!DOCTYPE html> <html> <head> <title></title> </head> <body> <input type="text" id="color"> <div id="errMsg"></div> <input type="button" value="try!" onclick="guessTheColors();"> </body> </html> */
9th Apr 2017, 12:53 AM
visph
visph - avatar
+ 8
you forgot the [ ] in the arr declaration. arr should not be an argument of the function, because you are giving the value in the function. function guessTheColors(response) { var arr = ["red","orange",...] ...} you are checking if the response is not a number, for every arr content, this is not necessary, do it once.
9th Apr 2017, 12:58 AM
Emore Anzolin
Emore Anzolin - avatar
+ 5
Thanks for the replys @EmoreAnzolin @shraddha @visph The solution that seems to work was the @visph and thanks for simplifying my code I figured I was cancelling out my statements somewhere.
9th Apr 2017, 3:00 AM
LeRoy Summers
LeRoy Summers - avatar
+ 3
The not statement is working fine. The problem is that your for loop continues to run even after it has found your matching color because you are missing break statements. I added break in 2 locations in your loop in the last 2 else ifs. function guessTheColors(response, arr) { response = document.getElementById('color').value; arr = ["red", "orange", "yellow", "green", "blue", "indigo", "violet", "RED", "ORANGE", "YELLOW", "GREEN", "BLUE", "INDIGO", "VIOLET", "Red", "Orange", "Yellow", "Green", "Blue", "Indigo", "Violet"]; for(var i = 0; i <= arr.length; i++) { if (response === "") { var ans1 = document.getElementById('errMsg').innerHTML = ("Please enter a color!"); } else if (!isNaN(response)) { var ans2 = document.getElementById('errMsg').innerHTML = ("Sorry, no numeric values!"); } else if (response != arr[i]) { var ans3 = document.getElementById('errMsg').innerHTML = ("Sorry, that\'s not a color of the rainbow!"); break; } else if (response == arr[i]) { var ans4 = document.getElementById('errMsg').innerHTML = ("Correct! " + response + " is one of the colors of the rainbow."); break; } } }
9th Apr 2017, 12:53 AM
Shraddha
Shraddha - avatar