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

javascript - Why I can't use string.charCodeAt() at elements of reduce callback - Stack Overflow

programmeradmin1浏览0评论

I have the following code for trying to get the total sum of the charCodes of one word. (Why I need that isn't important for my question)

function codepoints(string) {
    return string.split("").reduce((arr, next) => arr.charCodeAt(0) + next.charCodeAt(0))
}
console.log(codepoints("abc"));

But JavaScript is giving me the error:

arr.charCodeAt is not a function

When asking the type of 'arr', it is a string. But why I can not use the method charCodeAt on it?

I have the following code for trying to get the total sum of the charCodes of one word. (Why I need that isn't important for my question)

function codepoints(string) {
    return string.split("").reduce((arr, next) => arr.charCodeAt(0) + next.charCodeAt(0))
}
console.log(codepoints("abc"));

But JavaScript is giving me the error:

arr.charCodeAt is not a function

When asking the type of 'arr', it is a string. But why I can not use the method charCodeAt on it?

Share edited Apr 15, 2019 at 13:30 Salman Arshad 273k84 gold badges444 silver badges534 bronze badges asked Apr 12, 2019 at 12:46 DoddleDoddle 951 silver badge7 bronze badges 1
  • .charCodeAt() returns a number, so the second time through the callback arr will be a number not a string. – Pointy Commented Apr 12, 2019 at 12:49
Add a ment  | 

3 Answers 3

Reset to default 6

You are making two mistakes.

  • You don't need to apply charCodeAt on array its Number(sum of char codes).
  • Also pass 0 as second argument to reduce() which will be the initial value of arr

function codepoints(string) {
    return string.split('').reduce( (arr,next) => arr + next.charCodeAt(0),0)
}
console.log(codepoints("abc"));

Note: the variable name arr is not correct for situation. Use sum or ac or something like that.

You are not using Array.reduce correctly, the first parameter of the callback is the partial result or accumulator.

The Array.reduce takes a callback function which itself takes four more parameters, in your case you need the first two.

The syntax of the reduce function is reduce(callback[, initialValue]):

callback Function to execute on each element in the array, taking four arguments:

accumulator The accumulator accumulates the callback's return values; it is the accumulated value previously returned in the last invocation of the callback, or initialValue, if supplied (see below).

currentValue The current element being processed in the array.**

initialValue Optional Value to use as the first argument to the first call of the callback. If no initial value is supplied, the first element in the array will be used. Calling reduce() on an empty array without an initial value is an error.

To find the sum pass an initial value of 0 and then the add the subsequent elements in the array to it:

function codepoints(string) {
    return string.split('').reduce( (acc,next) => acc + next.charCodeAt(0), 0)
}

console.log(codepoints("abc"));

When you do not specify initialValue whel calling Array.reduce then:

  • accumulator will contain the first array element on first iteration
  • accumulator will contain the accumulated values on subsequent iterations

In your example you can check the accumulator variable and sum accordingly:

function codepoints(string) {
  return string
    .split("")
    .reduce((acc, next) => (typeof acc === "string" ? acc.charCodeAt(0) : acc) + next.charCodeAt(0))
}

console.log(codepoints("abc"));

发布评论

评论列表(0)

  1. 暂无评论