I implement a function that accepts a string and returns the position of each character in this string.

Function:

alphabet_position("The sunset sets at twelve o' clock.") 

Must return:

 "20 8 5 19 21 14 19 5 20 19 5 20 19 1 20 20 23 5 12 22 5 15 3 12 15 3 11" 

It is impossible to implement, my algorithm produces an error -1. What algorithm to invent to implement, I will not apply mind.

 function alphabetPosition(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 = "The sunset sets at twelve o' clock" var letterPosition = alphabetPosition(alphabet, letter); 

  • one
    because instead of a letter you pass the entire string in the letter parameter - Grundy
  • @Grundy, for sure, thanks. - spectre_it 2:51
  • in general, apparently, it is not the position of each character in this line that should be returned, but the position of each letter - Grundy
  • figured out. thank. added "his" solution for example. - spectre_it 3:49

3 answers 3

As an alternative solution, you can use a regular expression in the replace method.

There are two groups in it - characters to replace with numbers, and characters to replace with an empty string.

In the replacement function itself, check which range the character belongs in, capital or small letters, and in accordance with this output the required number

 function alphabetPosition(text) { var length = text.length, ACode = 65, aCode = 97, map = { true: aCode - 1, false: ACode - 1 }; return text.replace(/([az])|([^az])/ig, function($0, char) { if (!char) return ''; var charCode = char.charCodeAt(0); return (charCode - map[charCode >= aCode]) + ' '; }).trim(); } console.log(alphabetPosition("The sunset sets at twelve o' clock.")); 

    Your code still does not display the desired result, since you only have an alphabet for lowercase characters, and there are also capital letters (large) in the line.

    In order not to create an array with the alphabet, you can use the function String.prototype.charCodeAt , which returns the character code. The character code "a" (lower case English) is 97 :

      'a'.charCodeAt(0); // 97 

    Those. You can calculate the number of any letter in the English alphabet using symbol.charCodeAt(0) - 96 (a - 1, b - 2, c -3, etc.)

     var sentence = 'The sunset sets at twelve o' clock'; var result = sentence .toLowerCase() // ΠΏΡ€ΠΈΠ²ΠΎΠ΄ΠΈΠΌ всС символы Π² строкС ΠΊ Π½ΠΈΠΆΠ½Π΅ΠΌΡƒ рСгистру .replace(/[^az-]/g, '') // удаляСм всС ΠΊΡ€ΠΎΠΌΠ΅ Π±ΡƒΠΊΠ² az .split('') // ΠΏΡ€Π΅ΠΎΠ±Ρ€Π°Π·ΡƒΠ΅Ρ‚ строчку Π² массив .map(function(s) { return s.charCodeAt(0) - 96; // Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌ Π½ΠΎΠΌΠ΅Ρ€ Π±ΡƒΠΊΠ²Ρ‹ } ); // Π½Π° Π²Ρ‹Ρ…ΠΎΠ΄Π΅ ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ Π½Π΅ΠΎΠ±Ρ…ΠΎΠ΄ΠΈΠΌΡ‹ΠΉ Π²Π°ΠΌ массив console.log(JSON.stringify(result)); // "20 8 5 19 21 14 19 5 20 19 5 20 19 1 20 20 23 5 12 22 5 15 3 12 15 3 11" 

      returns the position of each character in the string:

       function alphabetPosition(text) { return text.toLowerCase().split('') .filter( c => c >= 'a' & c <= 'z' ) .map( c => c.charCodeAt(0) - 'a'.charCodeAt(0) + 1) .join(' '); } console.log(alphabetPosition('n8_ovuu&'));