Ad
  • Custom User Avatar

    There's probably no performance benefit over a well written loop. But the standard algorithms are allways better than a bad loop. The real benefit is in "readability of intention". In nested loops it's hard to see what was intended, the standard algorithms state pretty well what they are doing, e.g. copy something if -a condition is true- and you can be sure they do that correctly and efficiently. I must admit that the (nested) lambdas look a little scary at first, but once you get the hang of it the code becomes easier to understand.

  • Default User Avatar

    interesting answer, thank you. But it looks mush more complicated than a straightforward two loops with pushing back into vector. Simplier solution is much fatser to read. Is there any performance benefits I simply don't know about?

  • Default User Avatar

    Yah good eye, I may have to retract that comment, not sure what happened but it isn't working in chrome now.

    I might have been messing with polyfills, ah well it would be nice syntax, swiss army array objects.

    Also it is a little silly, it is a triangle function so it will only ever have the 3 arguments, but I was hoping to experiment with lists in js more.

  • Default User Avatar

    Yes,that's clear now. thanks a lot!)

  • Custom User Avatar

    YES, It's necessary to split the string into Array of Characters, because we can't call .filter() on a String (See MDN's article on Array.prototype.filter()).

    If you understood the please let me know.

  • Default User Avatar

    Was interested with your commented solution. After some searching I understood, that I don't understand how could it work in Chrome.
    -In JS 'arguments' is not an Array but Array-like object, it has only 'length' property. To convert easily there is a variant using spread operator '...'.
    -When you use Math functions you can't call them without Math (i.e., Math.max(args)).
    -Math object doesn't have sum() function at all, the best practice I found is using reduce (just as you used above).
    I tried to adjust all that and here is a working modification (but imo it's not so elegant)
    function isTriangle(a,b,c){
    return Math.max(...arguments) < Array.prototype.reduce.call(arguments,(a,b)=> a+b)-Math.max(...arguments);
    }

    PS: never thought about max() function and I like your implementation.

  • Default User Avatar

    Do this works without spliting the string (because the string is inerable by it self) or it is really necessary? If yes, explain, please.