Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>The basic idea</h2> <p>You could sum all the numbers and then subtract the largestOne in the end</p> <pre><code>for (var i = 0; i &lt; arr.length; i += 1) { if (arr[i] &gt; largestNumber) { largestNumber = arr[i]; } sumTotal += arr[i]; } // now because the sumTotal includes the largestNumberAlso // to get the same result as in your code, you have to sumTotal -= largestNumber </code></pre> <h2>The whole code altogether</h2> <pre><code>var myArray = [4, 6, 24, 10, 1, 3]; var arrayAddition = function (arr) { var largestNumber = arr[0]; var sumTotal = 0; for (var i = 0; i &lt; arr.length; i += 1) { if (arr[i] &gt; largestNumber) { largestNumber = arr[i]; } sumTotal += arr[i]; } sumTotal -= largestNumber; if (largestNumber === sumTotal) { return 'The result is true because the sumTotal is ' + sumTotal + ' and the largestNumber is ' + largestNumber; } else { return 'Wrong, the sumtotal is ' + sumTotal + ' and the the largestNumber is ' + largestNumber; } }; </code></pre> <h2>Some small improvement</h2> <p>There is one more improvement to make your code slightly faster</p> <p>Instead of doing the loop like this</p> <pre><code>for( var i = 0; i &lt; arr.length; i += 1 ) { ... } </code></pre> <p>You can write it like this</p> <pre><code>for( var i = 0, len = arr.length; i &lt; len; i++ ) { ... } </code></pre> <p>And that way you can save some processor cycles because you don't need to lookup the length attribute every time</p> <h2>Some other funky improvement</h2> <pre><code>var myArray = [4, 6, 24, 10, 1, 3]; var arrayAddition = function (arr) { var largestNumber = arr[0]; // that could satisfy the equation var sumTotal = 0; for( var i = 0, len = arr.length; i &lt; len; i++ ) // change it only if it is bigger then the current sum, // this way the largest number may be incorect if it is // smaller then the sum, but we don't care for that // on the other hand we can save some cpu cycles by not // doing a 'largestNumber = arr[i]' operation // if we don't really need to if (arr[i] &gt; sumTotal) { largestNumber = arr[i]; } sumTotal += arr[i]; } sumTotal -= largestNumber; if (largestNumber === sumTotal) { return 'The result is true because the sumTotal is ' + sumTotal + ' and the largestNumber is ' + largestNumber; } else { return 'Wrong, the sumtotal is ' + sumTotal + ' and the the largestNumber is ' + largestNumber; } }; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload