0
"use strict";

let arr = readline().split(" "); // suppose inputs are 1 2 3 4 5

let res=0;

for(let i=0; i<arr.length; i++){
    res += arr[i];
}

print(res); // output 012345

I know if I iterate through all the array elements and convert them to Number like arr[index] = Number(arr[index]); then I can get rid from this concatenation and will get pure sum.

But is there any way to convert all of these array elements of Strings to Number directly? without any iteration?

Joseph
  • 5,644
  • 3
  • 18
  • 44
  • 2
    "But is there any way to convert all of these array elements of Strings to Number directly? without any iteration?" - computationally, no, that's impossible. – Dai Dec 12 '20 at 16:04
  • 1
    Also, prefer `parseInt( str, 10 )` over `new Number` or `Number`: https://stackoverflow.com/questions/4090518/what-is-the-difference-between-parseint-and-number (remember to always specify the radix with `parseInt` too). – Dai Dec 12 '20 at 16:06
  • 3
    I am a bit puzzled about the last part of your question - what do you mean by "On V8 engine" exactly? – rags2riches-prog Dec 12 '20 at 16:08
  • 2
    What has this to do specifically with V8? -- I removed the reference and the tag. Should be used only when there is something in the question that specifically relates to V8 – trincot Dec 12 '20 at 16:11
  • @trincot I assume they know they're using V8 exclusively and so can use V8-specific language extensions and library features that aren't typically available in a web-browser context. – Dai Dec 12 '20 at 16:12

3 Answers3

2

use Array.reduce:

let arr = readline().split(" "); // suppose inputs are 1 2 3 4 5

let res=arr.reduce((acc,e) => +e + acc,0);
Alan Omar
  • 4,023
  • 1
  • 9
  • 20
1

"But is there any way to convert all of these array elements of Strings to Number directly?

Computationally, no. That's impossible. Iteration must happen. Otherwise it's like asking how you can visit every person on earth without having to travel.

But if you want to simplify your code by using functional programming then use Array.prototype.map:

const arrayOfInts = readLine().split( ' ' ).map( e => parseInt( e, 10 ) );

Take this further by performing sum with Array.prototype.reduce (also adding a filter to exclude NaN values which would break the summing operation):

I've documented the data-type of each chained function's return value on each line:

const sum = readLine()                    // string
    .split( ' ' )                         // string[]
    .map( e => parseInt( e, 10 ) )        // ( number | NaN )[]
    .filter( n => !isNaN( n ) )           // number[]
    .reduce( ( acc, n ) => acc + n );     // number
Dai
  • 141,631
  • 28
  • 261
  • 374
0

You can not do it without iterating over it. However, you already loop through the array, so...

Just convert them in the loop where you add them:

"use strict";

let arr = readline().split(" "); // suppose inputs are 1 2 3 4 5

let res=0;

for(let i=0; i<arr.length; i++){
    res += Number(arr[i]);
}

print(res); // output 15
FZs
  • 16,581
  • 13
  • 41
  • 50
  • I'm concerned that `res += +arr[i];` is too subtle (and it looks like a typo) to someone casually skimming the code. – Dai Dec 12 '20 at 16:08
  • You can add a comment there... – FZs Dec 12 '20 at 16:09
  • Yes, but writing good software code is like telling a joke: if you have to explain it then it's bad. – Dai Dec 12 '20 at 16:10
  • `res += Number(arr[i])`? – FZs Dec 12 '20 at 16:11
  • `parseInt` (with an explicit radix) should be preferred over `Number()`: https://stackoverflow.com/questions/4090518/what-is-the-difference-between-parseint-and-number – Dai Dec 12 '20 at 16:13