最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to correctly use Array.map() for replacing string with alphabet position - Stack Overflow

programmeradmin3浏览0评论

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()?

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()?

Share Improve this question asked Mar 26, 2019 at 17:45 JimJim 2,3227 gold badges42 silver badges77 bronze badges 3
  • 1 "nothing at all" really? – Jonas Wilms 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 – Andrew Castellano 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. – Bergi Commented Mar 26, 2019 at 17:52
Add a ment  | 

4 Answers 4

Reset to default 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.

You've plicated 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;
}

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) + ' ', '')

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,

发布评论

评论列表(0)

  1. 暂无评论