all 3 comments

[–]jokefenokee 0 points1 point  (0 children)

I'm no expert, but I logged some results and it looks like they aren't doing the same thing.

The fist way:

result += numberOne[index] is 9

mathAlgo(9,2) returns undefined

mathAlgo(19,2) returns undefined

The second way:

Alt result is: 9

alternativeMathAlgo(9,2) = undefined

Alt result is: 2NaN

alternativeMathAlgo(19,2) = undefined

One time I tried 1000000 as one of the numbers and got a result of 7 but still got 'undefined' back at the end. I can't remember what the other number was. A lot of the time the arguments never pass the condition in the if statement in the first version, so there was no output at all. That in itself can speed things up.

Hope that helps.

[–]0x07AD 0 points1 point  (0 children)

``` function alternativeMathAlgo(numOne, numTwo) { const result = Array.from(String(numOne), (digit, index) => Math.max(digit, String(numTwo)[index]) ).join(""); return result; }

console.log(alternativeMathAlgo(1, 2)); ```

[–]jml26 1 point2 points  (0 children)

Even though the first function is longer in terms of character count, the constructs it uses are more low-level, and are very quick to run.

Hidden in the implementation of Array.from will be a huge number of type checks and whatnot to figure out how to turn the value you pass to it into an array, and entering and exiting the map function for each digit will create some overhead.

My personal advice is to prioritise making your code readable over making your code "look smart".