Is there a Javascript equivalent of the python \'for-else\' loop, so something like this:
searched = input(\"Input: \");
for i in range(5):
if i==searched:
You'll have to use the boolean. There's no for-else
in JavaScript.
A nice and short way to search would be to use Array.prototype.indexOf()
:
var search = function(num, arr){
var index = arr.indexOf(num);
if(index !== -1)
return "Match found: " + index;
}
return "No match found!";
};
Call it like this, for example:
console.log(search(4, [0,1,2,3,4]));