How to correctly use Array.map() for replacing string with alphabet positionHow can I format numbers as currency string in JavaScript?How to check empty/undefined/null string in JavaScript?How can I convert a string to boolean in JavaScript?How to check if a string “StartsWith” another string?How do I correctly clone a JavaScript object?How can I get query string values in JavaScript?How do I make the first letter of a string uppercase in JavaScript?How to replace all occurrences of a string?How to check whether a string contains a substring in JavaScript?Replace a letter with its alphabet position
Why did so many MPs not vote in Meaningful Vote 3?
The Sword in the Stone
Why do planes need a roll motion?
Why can't my huge trees be chopped down?
This message is flooding my syslog, how to find where it comes from?
Why does RPM for a fixed-pitch propeller change with an aircraft's pitch?
Does academia have a lazy work culture?
Problem in styling a monochrome plot
Is this photo showing a woman posing in the nude before teenagers real?
How do we explain the E major chord in this progression?
kids pooling money for Lego League and taxes
Suggestions for protecting jeans from saddle clamp bolt
Is it legal for private citizens to "impound" e-scooters?
Request for a Latin phrase as motto "God is highest/supreme"
Why did Saturn V not head straight to the moon?
Can the term divorcée apply if a woman has not only divorced, but subsequently remarried?
Heisenberg uncertainty principle in daily life
AC contactor 1 pole or 2?
Piece-drop Mate #3
Why isn't there a serious attempt at creating a third mass-appeal party in the US?
Get delta of days by current hour and added delta of days
Why isn't there a ";" after "do" in sh loops?
What to do when you reach a conclusion and find out later on that someone else already did?
How to avoid unconsciously copying the style of my favorite writer?
How to correctly use Array.map() for replacing string with alphabet position
How can I format numbers as currency string in JavaScript?How to check empty/undefined/null string in JavaScript?How can I convert a string to boolean in JavaScript?How to check if a string “StartsWith” another string?How do I correctly clone a JavaScript object?How can I get query string values in JavaScript?How do I make the first letter of a string uppercase in JavaScript?How to replace all occurrences of a string?How to check whether a string contains a substring in JavaScript?Replace a letter with its alphabet position
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom: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()?
javascript
add a comment |
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()?
javascript
1
"nothing at all" really?
– Jonas Wilms
Mar 26 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
Mar 26 at 17:51
1
I guess you are looking foralphabet.indexOf(element)+1
instead ofalphabet.indexOf(element+1)
. Other than that, it should work.
– Bergi
Mar 26 at 17:52
add a comment |
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()?
javascript
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()?
javascript
javascript
asked Mar 26 at 17:45
JimJim
776 bronze badges
776 bronze badges
1
"nothing at all" really?
– Jonas Wilms
Mar 26 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
Mar 26 at 17:51
1
I guess you are looking foralphabet.indexOf(element)+1
instead ofalphabet.indexOf(element+1)
. Other than that, it should work.
– Bergi
Mar 26 at 17:52
add a comment |
1
"nothing at all" really?
– Jonas Wilms
Mar 26 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
Mar 26 at 17:51
1
I guess you are looking foralphabet.indexOf(element)+1
instead ofalphabet.indexOf(element+1)
. Other than that, it should work.
– Bergi
Mar 26 at 17:52
1
1
"nothing at all" really?
– Jonas Wilms
Mar 26 at 17:49
"nothing at all" really?
– Jonas Wilms
Mar 26 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
Mar 26 at 17:51
I think you're at least going to need to remove the 'element = ' portion of the second-to-last line
– Andrew Castellano
Mar 26 at 17:51
1
1
I guess you are looking for
alphabet.indexOf(element)+1
instead of alphabet.indexOf(element+1)
. Other than that, it should work.– Bergi
Mar 26 at 17:52
I guess you are looking for
alphabet.indexOf(element)+1
instead of alphabet.indexOf(element+1)
. Other than that, it should work.– Bergi
Mar 26 at 17:52
add a comment |
4 Answers
4
active
oldest
votes
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,
add a comment |
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, return
ing the position is enough.
add a comment |
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;
how isstr
the positions of each letter of the string in the alphabet if you're usingtext.charCodeAt(i)
?
– Jim
Mar 26 at 18:04
text.charCodeAt(i)
will return the char code of the character in the positioni
. If your string isjim
, thentext.charCodeAt(0)
will return the char code ofj
– varun agarwal
Mar 26 at 18:07
the position of 'j' in the alphabet is 10, however...
– Jim
Mar 26 at 18:10
1
Yeah, hence the- 96
to normalize the values.
– varun agarwal
Mar 26 at 18:12
add a comment |
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) + ' ', '')
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55363340%2fhow-to-correctly-use-array-map-for-replacing-string-with-alphabet-position%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
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,
add a comment |
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,
add a comment |
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,
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,
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"));
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"));
answered Mar 26 at 18:00
Miroslav GlamuzinaMiroslav Glamuzina
2,8922 gold badges13 silver badges23 bronze badges
2,8922 gold badges13 silver badges23 bronze badges
add a comment |
add a comment |
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, return
ing the position is enough.
add a comment |
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, return
ing the position is enough.
add a comment |
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, return
ing the position is enough.
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, return
ing the position is enough.
answered Mar 26 at 17:48
Jonas WilmsJonas Wilms
75.9k7 gold badges42 silver badges67 bronze badges
75.9k7 gold badges42 silver badges67 bronze badges
add a comment |
add a comment |
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;
how isstr
the positions of each letter of the string in the alphabet if you're usingtext.charCodeAt(i)
?
– Jim
Mar 26 at 18:04
text.charCodeAt(i)
will return the char code of the character in the positioni
. If your string isjim
, thentext.charCodeAt(0)
will return the char code ofj
– varun agarwal
Mar 26 at 18:07
the position of 'j' in the alphabet is 10, however...
– Jim
Mar 26 at 18:10
1
Yeah, hence the- 96
to normalize the values.
– varun agarwal
Mar 26 at 18:12
add a comment |
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;
how isstr
the positions of each letter of the string in the alphabet if you're usingtext.charCodeAt(i)
?
– Jim
Mar 26 at 18:04
text.charCodeAt(i)
will return the char code of the character in the positioni
. If your string isjim
, thentext.charCodeAt(0)
will return the char code ofj
– varun agarwal
Mar 26 at 18:07
the position of 'j' in the alphabet is 10, however...
– Jim
Mar 26 at 18:10
1
Yeah, hence the- 96
to normalize the values.
– varun agarwal
Mar 26 at 18:12
add a comment |
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;
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;
answered Mar 26 at 17:54
varun agarwalvarun agarwal
1,0882 silver badges9 bronze badges
1,0882 silver badges9 bronze badges
how isstr
the positions of each letter of the string in the alphabet if you're usingtext.charCodeAt(i)
?
– Jim
Mar 26 at 18:04
text.charCodeAt(i)
will return the char code of the character in the positioni
. If your string isjim
, thentext.charCodeAt(0)
will return the char code ofj
– varun agarwal
Mar 26 at 18:07
the position of 'j' in the alphabet is 10, however...
– Jim
Mar 26 at 18:10
1
Yeah, hence the- 96
to normalize the values.
– varun agarwal
Mar 26 at 18:12
add a comment |
how isstr
the positions of each letter of the string in the alphabet if you're usingtext.charCodeAt(i)
?
– Jim
Mar 26 at 18:04
text.charCodeAt(i)
will return the char code of the character in the positioni
. If your string isjim
, thentext.charCodeAt(0)
will return the char code ofj
– varun agarwal
Mar 26 at 18:07
the position of 'j' in the alphabet is 10, however...
– Jim
Mar 26 at 18:10
1
Yeah, hence the- 96
to normalize the values.
– varun agarwal
Mar 26 at 18:12
how is
str
the positions of each letter of the string in the alphabet if you're using text.charCodeAt(i)
?– Jim
Mar 26 at 18:04
how is
str
the positions of each letter of the string in the alphabet if you're using text.charCodeAt(i)
?– Jim
Mar 26 at 18:04
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
– varun agarwal
Mar 26 at 18:07
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
– varun agarwal
Mar 26 at 18:07
the position of 'j' in the alphabet is 10, however...
– Jim
Mar 26 at 18:10
the position of 'j' in the alphabet is 10, however...
– Jim
Mar 26 at 18:10
1
1
Yeah, hence the
- 96
to normalize the values.– varun agarwal
Mar 26 at 18:12
Yeah, hence the
- 96
to normalize the values.– varun agarwal
Mar 26 at 18:12
add a comment |
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) + ' ', '')
add a comment |
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) + ' ', '')
add a comment |
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) + ' ', '')
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) + ' ', '')
answered Mar 26 at 17:56
BJRINTBJRINT
3761 silver badge8 bronze badges
3761 silver badge8 bronze badges
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55363340%2fhow-to-correctly-use-array-map-for-replacing-string-with-alphabet-position%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
"nothing at all" really?
– Jonas Wilms
Mar 26 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
Mar 26 at 17:51
1
I guess you are looking for
alphabet.indexOf(element)+1
instead ofalphabet.indexOf(element+1)
. Other than that, it should work.– Bergi
Mar 26 at 17:52