最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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(' ');
发布评论

评论列表(0)

  1. 暂无评论