Home > Enterprise >  Problem selecting unique words from and array
Problem selecting unique words from and array

Time:04-10

I have an array of two keywords (will soon have more) and when a word is typed, I have a pre-written response. So the problem I am facing is due to having similar words. I have 2 items stored in my array, mail and adminmail. When I type adminmail, I get the mail response. If some one can help, it would be greatly appreciated

keywords=new Array();
urls=new Array();
replymsg=new Array();

keywords[0]="mail";
urls[0]="mail.html";
replymsg[0]="Type yes and I will load the mail app!"; 

keywords[1]="adminmail";
urls[1]="adminmail.html";
replymsg[1]="Type yes and I will load the admin mail app!";


function reply() {
wordentered=document.myform.user.value.toLowerCase();
myurl="";

for (i=0;i<keywords.length;i  ) {
        if (wordentered.indexOf(keywords[i])>-1) {
            myurl=urls[i];
            makecomputersay(replymsg[i]);
            return;
        }
    }
}

CodePudding user response:

"wordentered.indexOf(keywords[i])>-1" will satisfy for both mail & adminmail. You can compare with equal operator. Something like below.

for (i=0;i<keywords.length;i  ) {
        if (wordentered === keywords[i]) {
            myurl=urls[i];
            makecomputersay(replymsg[i]);
            return;
        }
    }
}

Please check the how indexOf works here : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf

CodePudding user response:

You may find it easier to use an object. That way you can access the information you need through its keys rather than having to loop over an array. 1) It looks neater 2) It's more efficient. 3) You can access information using this format obj[word].url or obj[word].replymsg.

Note: you should also start declaring your variables properly with either let or const

// New object containing some sub-objects
// You can access each bit of information
// with obj[word].url etc
const obj = {
  mail: {
    url: 'mail.html',
    replymsg: 'Type yes and I will load the mail app!'
  },
  adminmail: {
    url: 'admilemail.html',
    replymsg: 'Type yes and I will load the admin mail app!'
  }
};

// Because you're new to JS you may not be
// familiar with the next terminology. I'm just using them to
// make the example work, but you will have to understand them
// at some point, so why not now.

// `querySelector` (link to documentation at the end of the
// answer) simply uses CSS selectors to pick up elements from the DOM
const wordentered = document.querySelector('input[name="user"]');
const button = document.querySelector('button')

// And I'm adding a simple click listener to the button
// that calls the `reply` function
button.addEventListener('click', reply, false);

// Log the message
function makecomputersay(msg) {
  console.log(msg);
}

function reply() {
  
  // Get the value from the input
  const word = wordentered.value.toLowerCase();
  
  // If that word exists as a key on the object...
  // (No loop required!)
  if (obj[word]) {
 
    // ...get the message
    const replymsg = obj[word].replymsg;
    
    // Call the logging function
    makecomputersay(replymsg);

  }

}
<input name="user" type="text">
<button type="button">Submit</button>

Postscript:

This maybe a little advanced but I thought I'd mention it. You can also use something called Destructuring assignment to get the values from the object.

So instead of:

const url = obj[word].url;
const replymsg = obj[word].replymsg

you can combine those two statements into one line:

const { url, replymsg } = obj[word];

Addition documentation

CodePudding user response:

Just adding to the previous answer:

wordentered.indexOf(keywords[i]) is not the right way to go here. What this does is, that it checks where in your entered word the keyword appears.

In your example that means:

wordentered = "adminmail"
keywords[0] = "mail"
// keyword "mail" appears at index 5 (6th letter) of wordentered
// wordentered.indexOf("mail") === 5


wordentered = "adminmail"
keywords[1] = "adminmail"
// keyword "adminmail" appears at index 0 (1st letter) of wordentered
// wordentered.indexOf("adminmail") === 0

So in both cases wordentered.indexOf(keywords[i]) is greater than -1. As said before the way to go here is to use the equal to operator.

  • Related