user2964960
user2964960

Reputation: 29

Last iteration returns undefined

When looping through this string, the alert prints out test 4 times (correctly) but then also prints "undefined" at the end. How do I make it doesn't return undefined.

This returns - undefinedCAFE ::

alert(match("abcdef", "CAfe"));


function match(string, pattern) {

    var patternUpperCase = pattern.toUpperCase();
    var stringUpperCase = string.toUpperCase();
    var stringConcatenate;
    var answer;

    for (var i = 0; i < patternUpperCase.length; i++) {
        if (patternUpperCase.charAt(i) != undefined) {
            if (patternUpperCase.charAt(i) >= 'A' && patternUpperCase.charAt(i) <= 'Z') {
                stringConcatenate += patternUpperCase.charAt(i);
                alert("test");
            }
        }
    }
    return stringConcatenate;
}

This is what the function needs to do: returns true if all the individual LETTERS of pattern appear in string (regardless of order) (case insensitive)

Examples match("abcdef","@C2D!") returns true match("abcdef","CAfe") returns true match("abcdef","CG") returns false

Upvotes: 1

Views: 762

Answers (4)

rohit_agarwal
rohit_agarwal

Reputation: 160

Mistakes: 1. stringConcatenate was not initialized. 2. your code was incomplete, as it didn't check the condition where the pattern's letter is present in the 'string'.

This should solve your problem:

function fooo(){
str = "abcdsdfef";
pattern = "d234g4f";
var patternUpperCase = pattern.toUpperCase();
var stringUpperCase = str.toUpperCase();
var stringConcatenate="";
var answer = true;

for (var i = 0; i < patternUpperCase.length; i++) {
    if (patternUpperCase.charAt(i) != undefined) {
        if (patternUpperCase.charAt(i) >= 'A' && patternUpperCase.charAt(i) <= 'Z') {
            stringConcatenate += patternUpperCase.charAt(i);
    if(stringUpperCase.indexOf(patternUpperCase.charAt(i)) == -1){
        answer = false;
        break;
    }
        }
    }
}
alert(answer);

}

Upvotes: 0

Tom&#225;š Zato
Tom&#225;š Zato

Reputation: 53129

Your error happens because you're contactenating uninitialised string with some other string.

This line (for the first iteration):

stringConcatenate += patternUpperCase.charAt(i);

Where stringConcatenate has not be initialised reads as:

stringConcatenate = "undefined" + patternUpperCase.charAt(i);

So do this in your code:

var stringConcatenate = "";

Small note to accessing characters of strings:
If you use String.charAt() method, your test for return value should be folowing:

if(somestring.charAt(x)!="") 
   ... valid

However, you can also access offsets in string via array operator [x]. This one indeed returs undefined:

if(typeof somestring[x]!="undefined")

Upvotes: 2

Dan Cameron
Dan Cameron

Reputation: 756

Set stringConcatenate as a string variable first.

alert(match("abcdef", "CAfe"));


function match(string, pattern) {

    var patternUpperCase = pattern.toUpperCase();
    var stringUpperCase = string.toUpperCase();
    var stringConcatenate = '';

    for (var i = 0; i < patternUpperCase.length; i++) {
        if (patternUpperCase.charAt(i) != undefined) {
            if (patternUpperCase.charAt(i) >= 'A' && patternUpperCase.charAt(i) <= 'Z') {
                stringConcatenate += patternUpperCase.charAt(i);
                console.log(stringConcatenate);
            }
        }
    }
    return stringConcatenate;
}

Upvotes: 0

Martijn
Martijn

Reputation: 16103

Why not use the regex functions?

alert( !/[^pattern]/.test(string) );

Or to make it comply with the @ sign:

alert( !/[^pattern]/.replace(/[^a-zA-Z]/, '').test(string) );

The last example removes everything apart from a-z (lower and upper), and then test if any forbidden characters are in the remaining string.

Because this will give a true when it finds a 'wrong' character, reverse it with the !

Upvotes: 0

Related Questions