animus
animus

Reputation: 40

Trying to create a function which displays the name on passing the initials in Javascript

Trying to run javascript code which matches initials to the corresponding names in an array of objects.

I'm not able to execute the following code (I get no output. Not even the "Not Found" message. Also, no errors are shown up).

var chars = [
    {name:"Eddard Stark", alias: ["Hand of the King"]},
    {name:"Robert Baratheon", alias:["King"]}
];

function getName(initials)
{
    for(var x in chars){
        var check= str.split(chars[x].name);
        var flag=1;        
        for(var y in check){
            if(check[y][0]!==initials[y]){
                flag=0;
        }
    }
    if(flag)
        window.alert(chars[x].name+" "+chars[x].alias);
    else    
        window.alert("Not Found");
    }
}

getName("ES");

I have tried it in JSFiddle, as well as a HTML file with a <script> tag, opened in Internet Explorer (11). JSHint throws up no errors. What could be the problem?

Upvotes: 1

Views: 56

Answers (3)

Various errors: fix the split() function syntax, use boolean for flag, use charAt() to get characters from string (which you shouldn't treat like arrays), break out of y-loop as soon as a character mismatch is found, return the name as soon as a match is found without waiting for the x-loop to end.

var chars = [
    {name: "Eddard Stark", alias: "Hand of the King"},
    {name: "Robert Baratheon", alias: "King"}
];

function getName(initials) {
    for (var x in chars) {
        var check = chars[x].name.split(" ");
        var flag = true;        
        for (var y in check) {
            if (check[y].charAt(0) !== initials.charAt(y)) {
                flag = false;
                break;
            }
        }
        if (flag) return chars[x].name + ", " + chars[x].alias;
    }
    return "not found";
}

alert("ES: " + getName("ES"));
alert("XY: " + getName("XY"));

Upvotes: 0

Ketan Malhotra
Ketan Malhotra

Reputation: 1250

This seems to be working just fine:

var chars = [
    {name:"Eddard Stark", alias: ["Hand of the King"]},
    {name:"Robert Baratheon", alias:["King"]}
];

function getName(initials)
{
    var output;
    for(var x in chars){
        var check= chars[x].name.split(" ");
        var flag=1;        
        for(var y in check){
            if(check[y][0]!==initials[y])
                flag=0;
        }
        if(flag) {
            window.alert(chars[x].name+" "+chars[x].alias);
            return;
        }               
    }
    window.alert("Not Found");
}

getName("ES");

What this program is doing now is that it will search for a match through out the list and if it finds the match, it will display the result and then return; otherwise, it will continue to look for a match and if unable to find it will give Not Found! message.

Upvotes: 0

vinayakj
vinayakj

Reputation: 5681

Looks like you misunderstood the .spilt function.

var chars = [
    {name:"Eddard Stark", alias: ["Hand of the King"]},
    {name:"Robert Baratheon", alias:["King"]}
];

function getName(initials){
    for(var x in chars){
        var check = chars[x].name.split(' ');
         if((check[0][0]+check[1][0]).toUpperCase()==initials.toUpperCase())
           return alert(chars[x].name+" "+chars[x].alias);
    }
    alert("Not Found");   
}

getName("RB");

Upvotes: 1

Related Questions