I have written this JavaScript code where I have defined an array and took input from user and then compared the input and value from an array. On successful checking the URL will be concatenated with the correct input of the user but if the user provides wrong input, which is not within the array, then it will execute the else
part of the if
–else
statement. The code is successful until the initialization of i = 0
, but then it’s not working for the other values other than the 0th position and always goes to the else
part. What am I doing wrong?
var myStringArray = [ "youth", "robinson", "volvo", "bmw" ];
var arrayLength = myStringArray.length;
var url = "https://abcd.com/";
var company = prompt("Input your company Code to login");
for (var i = 0; i < arrayLength; i++) {
var name = myStringArray[i];
if (name == company) {
window.open(url.concat(company));
}
else {
alert("Company Code is wrong Try again from login");
}
break;
}