function - Form to find value's position in a javascript array - Stack Overflow

I am trying to create a form that takes only a single letter and finds its position in an array (which

I am trying to create a form that takes only a single letter and finds its position in an array (which is essentially the alphabet).

I wrote some this simple code:

var alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
var letter = "h";
var letterPosition = alphabet.indexOf(letter);

document.write(letterPosition);

to demonstrate the basic function. However, I am not sure how to put this into a function get and make the var letter equal to the value in the form's input.

I want this to return the location in the array so that I can write a loop (inside an if/else)that will print all values in the array that e after the input value.

I am trying to create a form that takes only a single letter and finds its position in an array (which is essentially the alphabet).

I wrote some this simple code:

var alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
var letter = "h";
var letterPosition = alphabet.indexOf(letter);

document.write(letterPosition);

to demonstrate the basic function. However, I am not sure how to put this into a function get and make the var letter equal to the value in the form's input.

I want this to return the location in the array so that I can write a loop (inside an if/else)that will print all values in the array that e after the input value.

Share Improve this question asked Mar 9, 2011 at 6:05 tehaarontehaaron 2,25010 gold badges31 silver badges54 bronze badges 2
  • What problem are you trying to solve? There are better ways of acplishing what you ask providing that they conform with your real problem. – HBP Commented Mar 9, 2011 at 6:18
  • @HansBPUFAL Here is the full scenario: I will have an editable table cell, someone will enter a letter into it, based on that letter all cells following it will be updated to the subsequent letters until "z" or the number of cells run out. I figured the alphabet array could be used in a for loop to acplish this. I have been trying to write each part independent of the other and then make them work together once i get a better grasp on each part. Maybe this isn't the best method? – tehaaron Commented Mar 9, 2011 at 6:26
Add a ment  | 

4 Answers 4

Reset to default 2

Try: alphabet[alphabet.indexOf(letter)] or in your code alphabet[letterPosition]

Concerning the loop you mentioned: with that value you can use the slice method to give you a subset of remaining characters from the alphabet array (ergo: no need for a loop to determine the remainder of elements from your array):

var subset = alphabet.slice(indexOf([a letter]));

And just to save you some typing: you could also declare your alphabet array like this:

var alphabet 'abcdefghijklmnopqrstuvwxyz'.split('');

Build a demo http://jsbin./ijuco4

Not sure of the context, is this loading with the page (no AJAX involved)? I may be oversimplifying:

<input type="text" id="whichLetter" />
<script type="text/javascript">

function getLetterPosition(allLetters, letter)
{
  return allLetters.indexOf(letter);
}

var alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
var letter = document.getElementById("whichLetter").value;
var letterPosition = getLetterPosition(alphabet, letter);

</script>

i love javascript.

or you could do

let alphabetPosition = (text) => text.toUpperCase().replace(/[^A-Z]/g, '').split('').map(ch => ch.charCodeAt(0) - 64).join(' ');

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745593165a4634951.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信