最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin5PV0评论

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(' ');
Post a comment

comment list (0)

  1. No comments so far