2

Other SO 'Replace string with alphabet positions' questions didn't utilize map, which is what I'm trying to learn how to use to solve this.

Problem: Given a string, replace every letter with its position in the alphabet. If anything in the text isn't a letter, ignore it and don't return it. "a" = 1, "b" = 2, etc.

What I've tried is: -looping over a new array instance and setting the index value to String.fromCharCode() - taking input string making it lowercase -splitting to array -return array.map().join(' ')

function alphabetPosition(text) {

let alphabet = new Array(26);
for (let i = 0; i<26; ++i) {
  let char = String.fromCharCode(97 + i);
  alphabet[i] = char;
}

text = text.toLowerCase();

let arr = text.split('');

return arr.map(element => { return element = alphabet.indexOf(element+1) }).join(' ');
}

expected it to return a string of alphabet positions, but got nothing at all. What is wrong with my implementation of Array.map()?

3
  • 1
    "nothing at all" really? Commented Mar 26, 2019 at 17:49
  • I think you're at least going to need to remove the 'element = ' portion of the second-to-last line Commented Mar 26, 2019 at 17:51
  • 1
    I guess you are looking for alphabet.indexOf(element)+1 instead of alphabet.indexOf(element+1). Other than that, it should work. Commented Mar 26, 2019 at 17:52

4 Answers 4

4

In your map element would be a letter, "a" for example. Then you add (concat) 1 to it, which results in "a1" which is not in your alphabet. Also element = is unneccessary, returning the position is enough.

Sign up to request clarification or add additional context in comments.

Comments

2

You've complicated the solution, the simplest approach would be to just find the charcode and return that.

function alphabetPosition(text) {
  let str = '';
  for (var i = 0; i < text.length; i++) {
    str += (text[i] + (text.charCodeAt(i) - 96));
  }
  return str;
}

3 Comments

how is str the positions of each letter of the string in the alphabet if you're using text.charCodeAt(i)?
text.charCodeAt(i) will return the char code of the character in the position i. If your string is jim, then text.charCodeAt(0) will return the char code of j
Yeah, hence the - 96 to normalize the values.
2

I totally understand that is a coding challenge, interview question or likewise so if you really need to use map() you should only return the result of the callback passed to map as follows :

return arr.map(x => alphabet.indexOf(x) + 1).join(' ')

However reduce() seems more appropriate in your case :

return arr.reduce((ac, cv) => ac + (alphabet.indexOf(cv) + 1) + ' ', '')

Comments

1

Your map() last line of the function was returning the value of an assignment.

return arr.map(element => { return element = alphabet.indexOf(element+1) }).join(' ');

Just alphabet.indexOf(element) would have sufficed.

This will give you the result you want:

alphabetPosition = text => {
  let alphabet = new Array(26);
  for (let i = 0; i < 26; ++i) {
    let char = String.fromCharCode(97 + i);
    alphabet[i] = char;
  }

  return text.toLowerCase().split('').map(element =>
    alphabet.indexOf(element)
  ).join(' ');
}

console.log(alphabetPosition("This is a string"));

Hope this helps,

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.