Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace a letter with its alphabet position

This looked fairly straightforward to me when I started, but for some reason I'm getting an empty array everytime I try to run the result on codewars. I'm hoping you can help me identify 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 strip out the spaces. I made a variable for my empty array and make an alphabet string that I can later search through. In the for loop, i make each character lowercase, and if the character is found in the alphabet string, its position gets pushed into the array (arr). I appreciate your time.

like image 264
Milos Avatar asked Jan 02 '17 12:01

Milos


People also ask

How do you get the alphabet position?

To get the position, subtract 96 from ASCII value of input character. In case if you convert input letter in to upper case, you need to subtract 64 as ASCII value for upper case alphabet stars from 65.

How do you get the numeric position of the alphabet in C++?

int position = 'g' - 'a' + 1; In C, char values are convertible to int values and take on their ASCII values. In this case, 'a' is the same as 97 and 'g' is 103. Since the alphabet is contiguous within the ASCII character set, subtracting 'a' from your value gives its relative position.

How do you replace letters in numbers?

Leet, or leetspeak, is a method of typing words using alternate characters. Letters are replaced with numbers or symbols that closely resemble them. For example, the letter "a" might be replaced with the @ symbol and the letter "E" might be replaced with the number 3. The word "leet" can be written as "1337."


Video Answer


2 Answers

The Kata works with this code. Try with this one:

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."));
like image 142
Praveen Kumar Purushothaman Avatar answered Oct 07 '22 14:10

Praveen Kumar Purushothaman


You need the 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"));

A solution with ES6

function alphabetPosition(text) {
    return [...text].map(a => parseInt(a, 36) - 10).filter(a => a >= 0);
}
console.log(alphabetPosition("Hello World!!1"));
like image 27
Nina Scholz Avatar answered Oct 07 '22 12:10

Nina Scholz