3

I have array of 1000 values.

var city = city_str.split(',');

It is cities in alphabet order.

I have function which search first word who started in letters which is declared in the argument.

function searchWord(letter){
 letter = letter.toUpperCase();
 var letters = city.map(n => n[0]);
 var word = '';
 var flag = false;
 for (var i = 0; (i<= letters.length)&&(flag); i++){
 if (letter == letters[i]) {
 word = city[i];
 flag = true;
 }
 }
 if (word == '') {
 return 0;
 }
 else return word;
}

but always return 0. why?

asked Jul 21, 2018 at 11:48

2 Answers 2

1

Your for loop condition (i<= letters.length)&&(flag) is false right at the outset, because you've required that flag be true, but you've set flag to false just prior to the loop. Remember, && means "and".

You probably meant !flag. Also, notice that you want <, not <=, when comparing i to letters.length. So: i < letters.length && !flag

Also note that you don't need those parens, and can I respectfully suggest consistent use of {} and indentation. So:

function searchWord(letter){
 letter = letter.toUpperCase();
 var letters = city.map(n => n[0]);
 var word = '';
 var flag = false;
 for (var i = 0; !flag && i < letters.length; i++) {
 if (letter == letters[i]) {
 word = city[i];
 flag = true;
 }
 }
 if (word == '') {
 return 0;
 } else {
 return word;
 }
}

Or you could just return from within the loop and avoid flag (and word) entirely:

function searchWord(letter){
 letter = letter.toUpperCase();
 var letters = city.map(n => n[0]);
 for (var i = 0; i < letters.length; i++) {
 if (letter == letters[i]) {
 return city[i];
 }
 }
 return 0;
}

In fact, you can avoid for entirely, because if city is an array (I assume from map), you can use find:

function searchWord(letter){
 letter = letter.toUpperCase();
 return city.find(n => n[0] == letter) || 0;
}

find calls your callback repeatedly until it returns a truthy value or it runs out of entries. If your callback returns a truthy value, find's return value is the entry for which the callback did that; otherwise, it returns undefined. Since we know the word in city won't be falsy, we can safely use || 0 to convert that undefined to 0.

answered Jul 21, 2018 at 11:51
Sign up to request clarification or add additional context in comments.

Comments

0

It is because your loop is not executing due to condition :

for (var i = 0; (i<= letters.length)&&(flag); i++)

flag is set to false. Therefor your loop will not execute.

Change this line to :

for (var i = 0; (i< letters.length)&&(!flag); i++)
answered Jul 21, 2018 at 11:50

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.