Feivi99
asked on
javascript replace letters with numbers
Hello,
I'm trying to filter an input value against a list of words, but I don't want that list of words to be easily seen by someone perusing my site. I therefore want to first want to define each letter as a number, and then have an array with my "numberwords". So, for example, I need to define that a=1, b=2, etc etc. Then I have a list of numbers, which are actually fruit; i.e. an array which looks like this:"1-16-16-12-5, 2-5-1-18, 15-18-1-14-7-5" which is actually the words "apple, pear orange". A user will then enter a word or sentence in an input, and I need to check whether the word or a word in the sentence equals any of the fruits mentioned above.
Any help greatly appreciated!
I'm trying to filter an input value against a list of words, but I don't want that list of words to be easily seen by someone perusing my site. I therefore want to first want to define each letter as a number, and then have an array with my "numberwords". So, for example, I need to define that a=1, b=2, etc etc. Then I have a list of numbers, which are actually fruit; i.e. an array which looks like this:"1-16-16-12-5, 2-5-1-18, 15-18-1-14-7-5" which is actually the words "apple, pear orange". A user will then enter a word or sentence in an input, and I need to check whether the word or a word in the sentence equals any of the fruits mentioned above.
Any help greatly appreciated!
ASKER
Thanks for the pointers, put if you could give me some code as to how to do all that, that would be great! I'm not so good with arrays etc...
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Try something like this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_27422674.html</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script>
jQuery(document).ready( function () {
$("#decode").click( function() {
var list = $("#encoded").val().split( "," );
var words = "";
var i = -1;
while ( ++i != list.length ) {
words += (i == 0)? "" : ",";
var word = list[i].split( "-" );
var j = -1;
while ( ++j != word.length ) {
words += String.fromCharCode( 64 + Number( word[j] ) );
}
}
$("#decoded").val( words );
})
});
</script>
</head>
<body>
<input id="encoded" value="1-16-16-12-5, 16-5-1-18, 15-18-1-14-7-5" size="48"/>
<input id="decode" type="button" value="Decode" />
<input id="decoded" readonly="true" size="48" />
</body>
</html>
ASKER
CKY092,
Thanks for putting that code together. The only thing is, I want the array of words already to be in number format. See in the question above
" Then I have a list of numbers, which are actually fruit; i.e. an array which looks like this:"1-16-16-12-5, 2-5-1-18, 15-18-1-14-7-5" which is actually the words "apple, pear orange". A user will then enter a word or sentence in an input, and I need to check whether the word or a word in the sentence equals any of the fruits mentioned above."
Thanks for putting that code together. The only thing is, I want the array of words already to be in number format. See in the question above
" Then I have a list of numbers, which are actually fruit; i.e. an array which looks like this:"1-16-16-12-5, 2-5-1-18, 15-18-1-14-7-5" which is actually the words "apple, pear orange". A user will then enter a word or sentence in an input, and I need to check whether the word or a word in the sentence equals any of the fruits mentioned above."
ASKER
Proculopsis,
Can you explain where I would put the array to be checked against?
Thanks
Can you explain where I would put the array to be checked against?
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Bump
ASKER
Perfect :-)
Thanks!
Thanks!
then convert the input word to number format.
then compare it