Using CharCodeAt with index not sentence












0















This question is previously asked by different ways but not same as this one. I want to achieve by Using ES5 or ES6 to write a function that calculates the word with the highest score using the system A = 1, B = 2, C = 3 etc. The string will only contain a single space between words and there will be no punctuation.



I have came up with this function.



var wordScoreCalculator = s =>
s.toLowerCase().
split('').
map(s => s.charCodeAt(0)-0x60).
filter(c => 1 <= c && c <= 26).
reduce((x,y) => x+y, 0);

wordScoreCalculator('I live in this world');


Currently, charCodeAt is mapping with whole sentence and it calculating all words together to 208.



I want to make it to work with index so it calculate each word individually and display the highest score only.



In this case, it should display 72. How this can be achieved?



Many thanks !










share|improve this question























  • Perhaps change the letter s to w, in the parameter given to the anonymous function in map?

    – Kristjan Kica
    Nov 20 '18 at 11:37
















0















This question is previously asked by different ways but not same as this one. I want to achieve by Using ES5 or ES6 to write a function that calculates the word with the highest score using the system A = 1, B = 2, C = 3 etc. The string will only contain a single space between words and there will be no punctuation.



I have came up with this function.



var wordScoreCalculator = s =>
s.toLowerCase().
split('').
map(s => s.charCodeAt(0)-0x60).
filter(c => 1 <= c && c <= 26).
reduce((x,y) => x+y, 0);

wordScoreCalculator('I live in this world');


Currently, charCodeAt is mapping with whole sentence and it calculating all words together to 208.



I want to make it to work with index so it calculate each word individually and display the highest score only.



In this case, it should display 72. How this can be achieved?



Many thanks !










share|improve this question























  • Perhaps change the letter s to w, in the parameter given to the anonymous function in map?

    – Kristjan Kica
    Nov 20 '18 at 11:37














0












0








0








This question is previously asked by different ways but not same as this one. I want to achieve by Using ES5 or ES6 to write a function that calculates the word with the highest score using the system A = 1, B = 2, C = 3 etc. The string will only contain a single space between words and there will be no punctuation.



I have came up with this function.



var wordScoreCalculator = s =>
s.toLowerCase().
split('').
map(s => s.charCodeAt(0)-0x60).
filter(c => 1 <= c && c <= 26).
reduce((x,y) => x+y, 0);

wordScoreCalculator('I live in this world');


Currently, charCodeAt is mapping with whole sentence and it calculating all words together to 208.



I want to make it to work with index so it calculate each word individually and display the highest score only.



In this case, it should display 72. How this can be achieved?



Many thanks !










share|improve this question














This question is previously asked by different ways but not same as this one. I want to achieve by Using ES5 or ES6 to write a function that calculates the word with the highest score using the system A = 1, B = 2, C = 3 etc. The string will only contain a single space between words and there will be no punctuation.



I have came up with this function.



var wordScoreCalculator = s =>
s.toLowerCase().
split('').
map(s => s.charCodeAt(0)-0x60).
filter(c => 1 <= c && c <= 26).
reduce((x,y) => x+y, 0);

wordScoreCalculator('I live in this world');


Currently, charCodeAt is mapping with whole sentence and it calculating all words together to 208.



I want to make it to work with index so it calculate each word individually and display the highest score only.



In this case, it should display 72. How this can be achieved?



Many thanks !







javascript ecmascript-6






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 11:34









WebappsdevaWebappsdeva

709




709













  • Perhaps change the letter s to w, in the parameter given to the anonymous function in map?

    – Kristjan Kica
    Nov 20 '18 at 11:37



















  • Perhaps change the letter s to w, in the parameter given to the anonymous function in map?

    – Kristjan Kica
    Nov 20 '18 at 11:37

















Perhaps change the letter s to w, in the parameter given to the anonymous function in map?

– Kristjan Kica
Nov 20 '18 at 11:37





Perhaps change the letter s to w, in the parameter given to the anonymous function in map?

– Kristjan Kica
Nov 20 '18 at 11:37












1 Answer
1






active

oldest

votes


















1














You need to additionally map each word, splitting on a space first. Also, because, per the condition The string will only contain a single space between words and there will be no punctuation, there's no need for the filter, since it sounds like the words will always contain alphabetical characters:






var wordScoreCalculator = s =>
s.toLowerCase()
.split(' ')
.map(word => word
.split('')
.map(char => char.charCodeAt(0)-0x60)
.reduce((x,y) => x+y, 0)
)
.reduce((a, b) => Math.max(a, b))

console.log(wordScoreCalculator('I live in this world'));
console.log(wordScoreCalculator('I live in this world zzzz'));





Or maybe abstract the operation that maps words to their value into its own function, for better readability:






const wordToScore = word => word
.split('')
.map(char => char.charCodeAt(0)-0x60)
.reduce((x,y) => x+y, 0);
const bestWordCalculator = s =>
s.toLowerCase()
.split(' ')
.map(wordToScore)
.reduce((a, b) => Math.max(a, b));

console.log(bestWordCalculator('I live in this world'));
console.log(bestWordCalculator('I live in this world zzzz'));








share|improve this answer


























  • Somewhat of a formatting improvement or refactor (depends on how you want to look at it) would be to preserve OP's initial function and then make a new one for sentences: sentenceScoreCalculator => s => s.split(' ').map(wordScoreCalculator)

    – vlaz
    Nov 20 '18 at 11:42











  • Is this ES5 or ES6 ?

    – Webappsdeva
    Nov 20 '18 at 11:45











  • @Webappsdeva it's ES6 but only due to the arrow functions.

    – vlaz
    Nov 20 '18 at 11:46











  • @Webappsdeva Your original code is in ES6 as well. (To turn into ES5, use Babel and polyfills, as always)

    – CertainPerformance
    Nov 20 '18 at 11:47











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53392123%2fusing-charcodeat-with-index-not-sentence%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You need to additionally map each word, splitting on a space first. Also, because, per the condition The string will only contain a single space between words and there will be no punctuation, there's no need for the filter, since it sounds like the words will always contain alphabetical characters:






var wordScoreCalculator = s =>
s.toLowerCase()
.split(' ')
.map(word => word
.split('')
.map(char => char.charCodeAt(0)-0x60)
.reduce((x,y) => x+y, 0)
)
.reduce((a, b) => Math.max(a, b))

console.log(wordScoreCalculator('I live in this world'));
console.log(wordScoreCalculator('I live in this world zzzz'));





Or maybe abstract the operation that maps words to their value into its own function, for better readability:






const wordToScore = word => word
.split('')
.map(char => char.charCodeAt(0)-0x60)
.reduce((x,y) => x+y, 0);
const bestWordCalculator = s =>
s.toLowerCase()
.split(' ')
.map(wordToScore)
.reduce((a, b) => Math.max(a, b));

console.log(bestWordCalculator('I live in this world'));
console.log(bestWordCalculator('I live in this world zzzz'));








share|improve this answer


























  • Somewhat of a formatting improvement or refactor (depends on how you want to look at it) would be to preserve OP's initial function and then make a new one for sentences: sentenceScoreCalculator => s => s.split(' ').map(wordScoreCalculator)

    – vlaz
    Nov 20 '18 at 11:42











  • Is this ES5 or ES6 ?

    – Webappsdeva
    Nov 20 '18 at 11:45











  • @Webappsdeva it's ES6 but only due to the arrow functions.

    – vlaz
    Nov 20 '18 at 11:46











  • @Webappsdeva Your original code is in ES6 as well. (To turn into ES5, use Babel and polyfills, as always)

    – CertainPerformance
    Nov 20 '18 at 11:47
















1














You need to additionally map each word, splitting on a space first. Also, because, per the condition The string will only contain a single space between words and there will be no punctuation, there's no need for the filter, since it sounds like the words will always contain alphabetical characters:






var wordScoreCalculator = s =>
s.toLowerCase()
.split(' ')
.map(word => word
.split('')
.map(char => char.charCodeAt(0)-0x60)
.reduce((x,y) => x+y, 0)
)
.reduce((a, b) => Math.max(a, b))

console.log(wordScoreCalculator('I live in this world'));
console.log(wordScoreCalculator('I live in this world zzzz'));





Or maybe abstract the operation that maps words to their value into its own function, for better readability:






const wordToScore = word => word
.split('')
.map(char => char.charCodeAt(0)-0x60)
.reduce((x,y) => x+y, 0);
const bestWordCalculator = s =>
s.toLowerCase()
.split(' ')
.map(wordToScore)
.reduce((a, b) => Math.max(a, b));

console.log(bestWordCalculator('I live in this world'));
console.log(bestWordCalculator('I live in this world zzzz'));








share|improve this answer


























  • Somewhat of a formatting improvement or refactor (depends on how you want to look at it) would be to preserve OP's initial function and then make a new one for sentences: sentenceScoreCalculator => s => s.split(' ').map(wordScoreCalculator)

    – vlaz
    Nov 20 '18 at 11:42











  • Is this ES5 or ES6 ?

    – Webappsdeva
    Nov 20 '18 at 11:45











  • @Webappsdeva it's ES6 but only due to the arrow functions.

    – vlaz
    Nov 20 '18 at 11:46











  • @Webappsdeva Your original code is in ES6 as well. (To turn into ES5, use Babel and polyfills, as always)

    – CertainPerformance
    Nov 20 '18 at 11:47














1












1








1







You need to additionally map each word, splitting on a space first. Also, because, per the condition The string will only contain a single space between words and there will be no punctuation, there's no need for the filter, since it sounds like the words will always contain alphabetical characters:






var wordScoreCalculator = s =>
s.toLowerCase()
.split(' ')
.map(word => word
.split('')
.map(char => char.charCodeAt(0)-0x60)
.reduce((x,y) => x+y, 0)
)
.reduce((a, b) => Math.max(a, b))

console.log(wordScoreCalculator('I live in this world'));
console.log(wordScoreCalculator('I live in this world zzzz'));





Or maybe abstract the operation that maps words to their value into its own function, for better readability:






const wordToScore = word => word
.split('')
.map(char => char.charCodeAt(0)-0x60)
.reduce((x,y) => x+y, 0);
const bestWordCalculator = s =>
s.toLowerCase()
.split(' ')
.map(wordToScore)
.reduce((a, b) => Math.max(a, b));

console.log(bestWordCalculator('I live in this world'));
console.log(bestWordCalculator('I live in this world zzzz'));








share|improve this answer















You need to additionally map each word, splitting on a space first. Also, because, per the condition The string will only contain a single space between words and there will be no punctuation, there's no need for the filter, since it sounds like the words will always contain alphabetical characters:






var wordScoreCalculator = s =>
s.toLowerCase()
.split(' ')
.map(word => word
.split('')
.map(char => char.charCodeAt(0)-0x60)
.reduce((x,y) => x+y, 0)
)
.reduce((a, b) => Math.max(a, b))

console.log(wordScoreCalculator('I live in this world'));
console.log(wordScoreCalculator('I live in this world zzzz'));





Or maybe abstract the operation that maps words to their value into its own function, for better readability:






const wordToScore = word => word
.split('')
.map(char => char.charCodeAt(0)-0x60)
.reduce((x,y) => x+y, 0);
const bestWordCalculator = s =>
s.toLowerCase()
.split(' ')
.map(wordToScore)
.reduce((a, b) => Math.max(a, b));

console.log(bestWordCalculator('I live in this world'));
console.log(bestWordCalculator('I live in this world zzzz'));








var wordScoreCalculator = s =>
s.toLowerCase()
.split(' ')
.map(word => word
.split('')
.map(char => char.charCodeAt(0)-0x60)
.reduce((x,y) => x+y, 0)
)
.reduce((a, b) => Math.max(a, b))

console.log(wordScoreCalculator('I live in this world'));
console.log(wordScoreCalculator('I live in this world zzzz'));





var wordScoreCalculator = s =>
s.toLowerCase()
.split(' ')
.map(word => word
.split('')
.map(char => char.charCodeAt(0)-0x60)
.reduce((x,y) => x+y, 0)
)
.reduce((a, b) => Math.max(a, b))

console.log(wordScoreCalculator('I live in this world'));
console.log(wordScoreCalculator('I live in this world zzzz'));





const wordToScore = word => word
.split('')
.map(char => char.charCodeAt(0)-0x60)
.reduce((x,y) => x+y, 0);
const bestWordCalculator = s =>
s.toLowerCase()
.split(' ')
.map(wordToScore)
.reduce((a, b) => Math.max(a, b));

console.log(bestWordCalculator('I live in this world'));
console.log(bestWordCalculator('I live in this world zzzz'));





const wordToScore = word => word
.split('')
.map(char => char.charCodeAt(0)-0x60)
.reduce((x,y) => x+y, 0);
const bestWordCalculator = s =>
s.toLowerCase()
.split(' ')
.map(wordToScore)
.reduce((a, b) => Math.max(a, b));

console.log(bestWordCalculator('I live in this world'));
console.log(bestWordCalculator('I live in this world zzzz'));






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 '18 at 11:45

























answered Nov 20 '18 at 11:38









CertainPerformanceCertainPerformance

81.5k143966




81.5k143966













  • Somewhat of a formatting improvement or refactor (depends on how you want to look at it) would be to preserve OP's initial function and then make a new one for sentences: sentenceScoreCalculator => s => s.split(' ').map(wordScoreCalculator)

    – vlaz
    Nov 20 '18 at 11:42











  • Is this ES5 or ES6 ?

    – Webappsdeva
    Nov 20 '18 at 11:45











  • @Webappsdeva it's ES6 but only due to the arrow functions.

    – vlaz
    Nov 20 '18 at 11:46











  • @Webappsdeva Your original code is in ES6 as well. (To turn into ES5, use Babel and polyfills, as always)

    – CertainPerformance
    Nov 20 '18 at 11:47



















  • Somewhat of a formatting improvement or refactor (depends on how you want to look at it) would be to preserve OP's initial function and then make a new one for sentences: sentenceScoreCalculator => s => s.split(' ').map(wordScoreCalculator)

    – vlaz
    Nov 20 '18 at 11:42











  • Is this ES5 or ES6 ?

    – Webappsdeva
    Nov 20 '18 at 11:45











  • @Webappsdeva it's ES6 but only due to the arrow functions.

    – vlaz
    Nov 20 '18 at 11:46











  • @Webappsdeva Your original code is in ES6 as well. (To turn into ES5, use Babel and polyfills, as always)

    – CertainPerformance
    Nov 20 '18 at 11:47

















Somewhat of a formatting improvement or refactor (depends on how you want to look at it) would be to preserve OP's initial function and then make a new one for sentences: sentenceScoreCalculator => s => s.split(' ').map(wordScoreCalculator)

– vlaz
Nov 20 '18 at 11:42





Somewhat of a formatting improvement or refactor (depends on how you want to look at it) would be to preserve OP's initial function and then make a new one for sentences: sentenceScoreCalculator => s => s.split(' ').map(wordScoreCalculator)

– vlaz
Nov 20 '18 at 11:42













Is this ES5 or ES6 ?

– Webappsdeva
Nov 20 '18 at 11:45





Is this ES5 or ES6 ?

– Webappsdeva
Nov 20 '18 at 11:45













@Webappsdeva it's ES6 but only due to the arrow functions.

– vlaz
Nov 20 '18 at 11:46





@Webappsdeva it's ES6 but only due to the arrow functions.

– vlaz
Nov 20 '18 at 11:46













@Webappsdeva Your original code is in ES6 as well. (To turn into ES5, use Babel and polyfills, as always)

– CertainPerformance
Nov 20 '18 at 11:47





@Webappsdeva Your original code is in ES6 as well. (To turn into ES5, use Babel and polyfills, as always)

– CertainPerformance
Nov 20 '18 at 11:47


















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53392123%2fusing-charcodeat-with-index-not-sentence%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

SQL update select statement

'app-layout' is not a known element: how to share Component with different Modules