Given is res[]:
for(let k = 0; k < res.length; k++){
if($("#getData").val().length == 20){
//if ((res[k]) != $("#getData").val()) helpText = "No match!";
// -- Tried this too and a several other options - foreach, else, else if e. g.
if(!((res[k]) == $("#getData").val()))
resText = "No match!";
if((res[k]) == $("#getData").val())
resText = "Success!";
$("#result").html(resText);
}
}
If the !((res[k]) == $("#getData").val()) - "false" condition is defined, it always only makes the first res[0] item within the array valid. This item is possible to proof correctly - The array donĀ“t iterate.
If the - "false" condition is not defined, all works fine if only the "true" condition is defined. But in this way there is no resText == "No match!";
What is wrong...?
As #charlietfl mentioned, you are looping across an array and updating the result text at each index. Because you are never stopping the looping process, what this really means is that you are only ever getting the result from the last item in the array. If your "Success!" value is item 14 in a 20 item array, your result will match and pass on that iteration, but then will be overwritten on the iterations for the next 5 items. If the very last item is a success condition you will always get "Success!" and if not you will always get "No match!" regardless of the other items in the list.
My assumption is that what you are looking to do is determine if one of the items in the array is a match, and if so you want "Success!" as the output. If so, I suggest:
const success = false;
const getDataVal = $("#getData").val();
if (getDataVal.length == 20) {
for(let k = 0; k < res.length; k++) {
if (res[k] == getDataVal) {
success = true;
break;
}
}
}
$("#result").html(success ? "Success!" : "No match!");
I restructured your code a bit here to help me understand it. Note that I did some optimizations to it:
Moved the $("#getData").val() operation out of the loop and stored to a variable since the value will not change. This simplifies the code and is much easier on the processor.
Moved the condition on the length check of the value outside the loop. The loop contents would only ever run whenever that condition is true, so it is better to wrap the loop in the condition so that it never runs at all if that condition fails.
Added a break; to the loop to stop the loop once a success condition is found
Moved the setting of the #result outside the loop. This only needs to be done once you know the answer and not at every iteration.
Use a ternary with a simple boolean instead of strings to know the status of the match and output the final string.
If my assumption is correct that you are really interested in knowing if any item in the array matches, then I suggest using the some method of the array to evaluate it. This is a bit more expressive and removes the need for the looping structure.
let success = false;
const getDataVal = $("#getData").val();
if (getDataVal.length == 20) {
success = res.some(function (val)) {
return val == getDataVal;
});
}
$("#result").html(success ? "Success!" : "No match!");
Related
I have an input field where the user inputs their zip code which I then attempt to match to a zip code within an array. While the following works, I need it to actually show an alert dialog once saying that zip code wasn't found, however, it current pops up a lot even if the zip code is in the list.
Here's my snippet:
_zipcodeBtn.onclick = function(){
var userZip = _zipcode.value;
for (var i = 0; i < zipcode_list.length; i++) {
if (zipcode_list[i] === userZip) {
console.log('works');
break;
} else {
alert("repeating");
}
}
};
I want it to check if their zip is available without it also repeating the else statement multiple times. Whats the best way to prevent this?
There's an easier way to find an item in an array, by referencing the array's indexOf() method (docs).
if (zipcode_list.indexOf(userZip) != -1) {
//found - do something
} else
alert('Zip not found!');
As Angel Politis shows, you can also use includes() if you literally just want to know whether an item is in an array, not its actual position within it.
Sidenote: it's important to check against -1 when using indexOf() because it returns the index at which the search is found - or -1 if it's not. If the search is found at the first key, this is 0, and 0 is a falsy value, which can catch people out sometimes when they do things like this:
var arr = [1, 2, 3];
if (arr.indexOf(1))
alert('success');
else
alert('failed');
You'd think the success alert would fire here, but actually it'll be the failure alert, because indexOf() in this case returns 0, and 0 is a falsy value when it's interrogated in a condition.
There's no need to use a loop here. You can just say:
if (!zipcode_list.includes(userZip)) alert("Doesn't work!");
If you must use a loop, then just set a flag by default to false and then, if the zip is found set it to true. Then you can check it outside the loop:
/* Create a flag and set it by default to false. */
var found = false;
/* Loop */
for (var i = 0, l = zipcode_list.length; i < l; i++) {
if (zipcode_list[i] === userZip) {
/* Set the flag to true and stop the loop. */
found = true;
break;
}
}
/* Check whether the zip was found in the array. */
if (found) console.log("Works!");
else alert("Doesn't work!");
I am having trouble returning a statement when my RegExp finds no matches :
function ai(message) {
if (username.length < 3) {
username = message;
send_message("Oh, well hello " + username + ", My name is Donald J Trump and i'm a big boy");
} else {
for (i = 0; i <= botChat.length; i++) {
var re = new RegExp(botChat[i][0], "i");
if (re.test(message)) {
var length = botChat[i].length - 1;
var index = Math.ceil(length * Math.random());
var reply = botChat[i][index];
send_message(reply);
}
}
}
}
When I enter a phrase it can correctly match the first line in an array as per the for loop. The issue I'm having is when I try to add an else statement it ceases to loop through my array properly.
I have tried :
else if (re.test(message) === false) {
send_message("i can't be expected to know everything");
}
But it doesn't work, it prevents the loop from looping past botChat[0][0].
I've also tried :
if (send_message().val() == "") {
// ...
}
But every time my code no longer works. Is there a method of adding something to my array that says 'if you don't find a match above, choose this'?
Or just a way of working my code so that the if/else works?
attached is the codepen.
I checked your codepen and the lines 190 and 194 console.log(send_message().val()); seems to be breaking the loop because those lines are throwing an exception since send_message() returns undefined and undefined does not have the .val() method.
Your regular expressions are working fine. I recommend not creating new RegExp objects every iteration, instead, use the one defined in the array if (botChat[i][0].test(message)), the overhead will be less.
I want to check if string is same from an array object or not before sending to a table form, but it can be the same if you didn't change anything in the edit zone. User fill down the table form and send it to the server, next time if user edit the form I want to check if the value are same as the array (which store the previous informations from the table), but it's ok if user didn't change anything but enter to the edit zone. The problem is my code checked that there's no same string but it popup either the true or the false alert message, it popup every message while checking the table form and go into the "false" statement, so I can send nothing after checking string. I will appreciate any help, thx! :)
var arr = [{words: a},{words: b},{words: c},{words: d}];
var val = $('#somethingFromHTML').val()
for(var i = 0; i < arr.length; i++) {
if (arr[i].words.indexOf(val) > -1){
alert("duplicate words")
return false
} else if (arr[i].words.indexOf(val) === -1 || arr[i].words === val){
alert("there's no duplicate words")
return true
}
}
You code always returns the result of checking only arr[0].words, it never moves on to arr[1].words at all, because
You have a return in both of your if blocks, and
Your second if condition is, in part, the inverse of the first one
Consequently, if val is in arr[0].words (indexOf returns > -1), the code will return false. Otherwise, it will return true, because by definition, indexOf returned -1 and your second condition is "if it's -1 or ...". The second part of the or is irrelevant, because the first part will be true.
The minimal change is to put the return true after the loop:
var arr = [{words: a},{words: b},{words: c},{words: d}];
var val = $('#somethingFromHTML').val();
for(var i = 0; i < arr.length; i++) {
if (arr[i].words.indexOf(val) > -1){
alert("duplicate words");
return false;
}
}
alert("there's no duplicate words");
return true;
...but arrays have a feature for just this situation: Array.prototype.some:
var arr = [{words: a},{words: b},{words: c},{words: d}];
var val = $('#somethingFromHTML').val();
if (arr.some(function(entry) { entry.words.indexOf(val) > -1})) {
alert("duplicate words");
return false;
} else {
alert("there's no duplicate words");
return true;
}
some calls its callback repeatedly with the entries in the array, in order, until the callback returns a truthy value. If the callback ever does that, some returns true; if it reaches the end of the array with the callback never having returned a truthy value, it returns false. So it's useful for checking if any entry in the array matches a condition.
I am having multiple issues with my <select> element in angular and am trying to understand what is going on. My first step is to understand why the multiple console.log() messages I have put in for debugging repeatedly appear in the console, as in, instead of the message appearing once like I would expect, they appear an infinite number of times, as if part of an infinite loop. Is this how a function called from an ng-options is supposed to behave? If so, I don't understand why, if not, then I would like to fix my loop.
My html: <select ng-options="theorderdate as theorderdate for theorderdate in getOtherOrderDates(Bread.text)" ng-model="randomDateRomantic.randomDateRomantic" ng-change="soRomantic(Bread.text)"></select>
The console.log() messages appear from the getOtherOrderDates() function, which is below (with my comments included):
$scope.getOtherOrderDates = function(loaf) {
var istheLoafdaily = false;
var theorderdates = [];
for (var i = 0; i < $scope.theBreadsList.length; i++) {
for (var z = 0; z < $scope.theBreadsList[i].breads.length; z++) {
if ($scope.theBreadsList[i].breads[z].text == loaf && i > 0) //not a daily loaf, goes beyond "Daily Breads"
{
console.log(theorderdates);
theorderdates = theorderdates.concat($scope.theBreadsList[i].breads[z].orderDates); //concat the matched bread's order dates
console.log(theorderdates, $scope.theBreadsList[i].breads[z].orderDates);
theorderdates = _.sortBy(theorderdates, function(m) {
return m.getTime()
});
for (var y = 0; y < theorderdates.length; y++) {
theorderdates[y] = theorderdates[y].toLocaleDateString();
}
theorderdates = _.uniq(theorderdates);
if (theorderdates.length > 0) {
console.log("Something is wrong here"); //problem
$scope.randomDateRomantic.randomDateRomantic = theorderdates[0];
}
console.log(theorderdates);
return theorderdates;
} else if ($scope.theBreadsList[i].breads[z].text == loaf && i == 0) { //a daily loaf, i == 0
console.log("The bread matched is daily", loaf); //***
istheLoafdaily = true;
console.log(theorderdates); //***
theorderdates = theorderdates.concat($scope.theBreadsList[i].breads[z].orderDates); // concat the matched bread's order dates
console.log(theorderdates, $scope.theBreadsList[i].breads[z].orderDates); //***
break; // escape the for loop, should it be two breaks?????? yes...
} else if (istheLoafdaily && i > 0 && $scope.theBreadsList[i].breads[z].orderDates.length > 0) { //not sure what scenario this matches, hence wtf
theorderdates = theorderdates.concat($scope.theBreadsList[i].breads[z].orderDates);
console.log("wtf");
}
}
}
//end of outermost for loop
//not sure what this is doing because this functionality is repeated up there^ (for non-daily breads)
theorderdates = _.sortBy(theorderdates, function(m) {
return m.getTime()
});
for (var y = 0; y < theorderdates.length; y++) {
theorderdates[y] = theorderdates[y].toLocaleDateString();
}
theorderdates = _.uniq(theorderdates);
if (theorderdates.length > 0) {
$scope.randomDateRomantic.randomDateRomantic = theorderdates[0];
console.log("Something is wrong here (daily)"); //problem
}
return theorderdates;
//not sure what this is doing because this functionality is repeated up there^ (for non-daily breads)
//if change to Locale date string then not unique, but if don't change then not a date to sort!!!!!!! >:(
},
I am getting almost all console messages an infinite number of times, without doing anything such as firing the ng-change function. I just add a daily bread to my cart for instance, and then the console gets filled with the following messages, that I have starred in my code.
My theBreadsList is not very long, so there is something going on that it is going repeatedly like this. Even if I broke out of the for loop twice as you will see in my code, it wouldn't explain the fact that it logs to the console all the time, because eventually the loop would not be satisfied, and this wouldn't take to long as has been mentioned.
Please advise, thank you. If you need more information, I am happy to provide.
The getOtherOrderDates will be called in each digest cycle so that angular knows whether to update options in select. That's most likely the reason you're seeing this method being called many times.
If you're worried about performance impact of this loop you can build the options upfront inside your controller store it in $scope like so:
$scope.options = $scope.getOtherOrderDates($scope.Bread.text);
whenever $scope.Bread.text changes and then use $scope.options inside your template.
To avoid triggering your loops in every digest loop you can use one time binding ::value.
<select ng-options="theorderdate as theorderdate for theorderdate in ::getOtherOrderDates(Bread.text)"
ng-model="randomDateRomantic.randomDateRomantic"
ng-change="soRomantic(Bread.text)"></select>
Thanks to that expression inside ng-options will be evaluated only once and the watcher will be removed after first evaluation which will stop your function being triggered in next digest loop iterations.
DOCS
I have had a lot of trouble with JavaScript and 'break' statements coming from a Ruby background.
Here is my function:
function isItTheNumber(numberToGuess){
if(previousGuess === null){
previousGuess = [playersGuess];
}
previousGuess.forEach(function(guess){
if(playersGuess === guess && previousGuess > 1){
textStatus.style.display = 'block';
textStatus.innerHTML = 'You already picked that number';
} else if(parseInt(playersGuess) === parseInt(numberToGuess)){
textStatus.style.display ='block';
textStatus.innerHTML = 'You Are CORRECT!';
} else {
previousGuess.push(playersGuess);
textStatus.style.display='block';
textStatus.innerHTML = 'You are ' + hotOrCold();
document.getElementById('guess-count').innerHTML = playerGuessCount++;
}
});
}
In my .forEach loop I would like to have a 'break' statement in my first if statement. I would like the loop to stop if it ever executes this block.
I realize I can't use a break statement in this forEach function after reading a few posts about it. I attempted the suggestion here using "every" but when I was using this wrapped in a function I was not able to return a true or false value.
I would like to avoid having to use any type of 'break' or hack for the break but will use it if it is the only way. If anyone has any suggestions on how I can re-work my logic or have any suggestions I would appreciate it. I'll list my logic in pseudo code below.
1) Check if the previousGuess array is null or populated. If it is null, set it to an array with the first value.
2) Iterate over the previousGuess array.
3) If: see if the user input (playerGuess) is in the previousGuess array. The previous guess
array must be larger than 1.
4) Else If: If the users guess is the same as the a the value, tell them they are correct.
5) Else: if the users guess isn't the same as the value, tell them and add 1 to the playerGuessCount.
The problem with my current logic is that the playerGuessCount is being invoked too many times. If the array is being iterated over and finds and the first if statement is true, it will still iterate over the rest of the array, adding 1 to the playerGuessCount even when they only submit 1 guess. The .forEach is strictly there to check if their guess is a repeat.
Here is my attempt with 'every' http://repl.it/P74
The .forEach method you are using is a wrapper implemented by extending a function prototype.
You can break out of a conventional loop:
for (var key in objs){
var obj=objs[key];
//do your business here
break; //this line exists the loop
}
However, if a callback function is used, you have to terminate the function call itself by placing a "skip" variable.
previousGuess.forEach(function(guess){
if (this.skip_the_foreach_loop) return;
if (need_to_exit) this.skip_the_foreach_loop=true;
});
Not the most efficient way but saves you a few CPU cycles.
Would this work for what you're trying to do?
function isItTheNumber(numberToGuess){
if(previousGuess === null){
previousGuess = [playersGuess];
}
// Using Array.prototype.every so a falsey return breaks
previousGuess.every(function(guess){
if(playersGuess === guess && previousGuess > 1){
textStatus.style.display = 'block';
textStatus.innerHTML = 'You already picked that number';
return; // returns undefined which is falsey and breaks loop.
} else if(parseInt(playersGuess) === parseInt(numberToGuess)){
textStatus.style.display ='block';
textStatus.innerHTML = 'You Are CORRECT!';
} else {
previousGuess.push(playersGuess);
textStatus.style.display='block';
textStatus.innerHTML = 'You are ' + hotOrCold();
document.getElementById('guess-count').innerHTML = playerGuessCount++;
}
return true; // Meaning go to next iteration
});
}