Replace a letter with a letter in alphabetical order

It looked pretty frank to me when I started, but for some reason I get an empty array every time I try to run the result on code tables. I hope you can help me determine what the problem is.

function alphabetPosition(text) { text.split(' ').join(''); var chari = ""; var arr = []; var alphabet = "abcdefghijklmnopqrstuvwxyz".split(''); for(var i = 0; i < text.len; i++){ chari = text.charAt(i).toLowerCase(); if(alphabet.indexOf(chari) > -1){ arr.push(alphabet.indexOf(chari)); } } return arr; } console.log(alphabetPosition("Hello World")); 

My idea is to get the text from the parameter, then cross out the spaces. I created a variable for my empty array and create an alphabet string that I can execute later. In the for loop, I make each character lowercase, and if the character is in the string of the alphabet, its position falls into the array (arr). I appreciate your time.

+5
source share
9 answers

Kata works with this code. Try with this:

 function alphabetPosition(text) { var result = ""; for (var i = 0; i < text.length; i++) { var code = text.toUpperCase().charCodeAt(i) if (code > 64 && code < 91) result += (code - 64) + " "; } return result.slice(0, result.length - 1); } console.log(alphabetPosition("The sunset sets at twelve o' clock.")); 
+4
source

You need String#length property

 text.length 

instead of text.len .

 function alphabetPosition(text) { var chari, arr = [], alphabet = "abcdefghijklmnopqrstuvwxyz", i; for (var i = 0; i < text.length; i++){ chari = text[i].toLowerCase(); if (alphabet.indexOf(chari) !== -1){ arr.push(alphabet.indexOf(chari)); } } return arr; } console.log(alphabetPosition("Hello World!!1")); 

Solution with ES6

 function alphabetPosition(text) { return [...text].map(a => parseInt(a, 36) - 10).filter(a => a >= 0); } console.log(alphabetPosition("Hello World!!1")); 
+3
source

First: delete space
Secondly: mapping each char with its alphabet rank
Third: test with the string Happy New Year

 var alphabet = "abcdefghijklmnopqrstuvwxyz".split(''); var alphabetPosition = text => text.split('').map(x => alphabet.indexOf(x) + 1); console.log(alphabetPosition("happy new year")); 
+3
source

This example will return based on an array based on 0 and will use lambda expressions with a filter. I recycle the original byte array created by splitting the text passed to the method.

 function alphabetPosition(text) { var bytes = text.split(''); var alphabet = "abcdefghijklmnopqrstuvwxyz".split(''); for (var i = 0, len = text.length; i < len; i++) { bytes[i] = alphabet.indexOf(bytes[i].toLowerCase()); } return bytes.filter(n => { if(n > -1) return n; } ).join(' '); } console.log(alphabetPosition("Hello World")); 

For the result of an array based on 1 Kata Codewars Friendly

 function alphabetPosition(text) { var bytes = text.split(''); var alphabet = "abcdefghijklmnopqrstuvwxyz".split(''); for (var i = 0, len = text.length; i < len; i++) { bytes[i] = alphabet.indexOf(bytes[i].toLowerCase()) + 1; } return bytes.filter(n => { if(n > 0) return n; } ).join(' '); } console.log(alphabetPosition("Hello World")); 
+1
source

change len to length :

 (var i = 0; i < text.len; i++) // change to (var i = 0; i < text.length; i++) 
0
source

You can make it even easier by using acii code. Because a = ascii code 97, b = 98, etc. And there is a javascript function String.charCodeAt( n ) that returns ascii code with a specific function. You only need to change the offset for capitals (if you want to support them).

 function alphabetPosition( text ) { var positions = []; for ( var i = 0; i < text.length; i++ ) { var charCode = text.charCodeAt( i ); if ( charCode >= 97 && charCode <= 122 ) { positions.push( charCode - 96 ); } else if ( charCode >= 65 && charCode <= 90 ) { // get rid of this if you don't care about capitals positions.push( charCode - 64 ); } } return positions; } var positions = alphabetPosition( 'Hello World' ); console.log(positions); 

Make this work fiddle

0
source

You can also use the .charCodeAt function:

 function alphabetPosition(text) { start = "a".charCodeAt(0); end = "z".charCodeAt(0); res = []; for (var i = 0; i < text.length; i++) { index = text.charAt(i).toLowerCase().charCodeAt(0); if (index >= start && index <= end) { res.push(index - start +1); // +1 assuming a is 1 instead of 0 } else { res.push(0); // Or do w/e you like with non-characters } } return res; } console.log(alphabetPosition("Hello World")); 
0
source

You can do something like this:

 var alpha = [].reduce.call("abcdefghijklmnopqrstuvwxyz0123456789 .,!",(p,c,i) => (p[c] = i,p),{}), str = "Happy 2017 whatever..!", coded = [].map.call(str, c => alpha[c.toLowerCase()]); console.log(coded); 
0
source

Here is a shorter version that does the same:

 function alphabetPosition(text){ return text.split('').map(function(character){ return character.charCodeAt(0) - 'a'.charCodeAt(0) + 1; }) } console.log(alphabetPosition("Hello World")); 
0
source

Source: https://habr.com/ru/post/1262176/


All Articles