Note that there are some explanatory texts on larger screens.

plurals
  1. POnumber that should be returned is the first or last in the array
    primarykey
    data
    text
    <p>i have got test scenarios for certain cases in my jasmine.. but i am not able to get the test cases for this three scenarios... can you tell me how to do it...</p> <ul> <li>the number that should be returned is the first or last in the array undefined values in the array (or NaN, Infinity, …)\ numbers with identical absolute values (e.g. -0.01 and 0.01)</li> </ul> <p>providing my fiddle too</p> <p><a href="http://jsfiddle.net/YYg8U/15/" rel="nofollow">http://jsfiddle.net/YYg8U/15/</a></p> <pre><code>var myNumbersToSort = [-1, 2, -3, 4, 0.3,1,-0.001]; function getClosestToZero(set) { if(0 === set.length) return null; var closest = Math.abs(set[0]), result = 0; for(var i in set) { var next = Math.abs(set[i]); if(closest &gt; next) { result = i; closest = next; } } return closest; } document.getElementById('output').innerHTML = (getClosestToZero(myNumbersToSort)); </code></pre> <p>jasmine</p> <pre><code>describe( 'getClosestToZero', function () { it( 'finds a positive unique number near zero', function () { expect( getClosestToZero( [-1, 0.5, 0.01, 3, -0.2] ) ).toBe( 0.01 ); } ); it( 'finds a negative unique number near zero', function () { expect( getClosestToZero( [-1, 0.5, -0.01, 3, -0.2] ) ).toBe( -0.01 ); } ); // your method actually doesn't pass this test // think about what your method *should* do here and if needed, fix it it( 'finds one of multiple identical numbers near zero', function () { expect( getClosestToZero( [-1, 0.5, 0.01, 0.01, 3, -0.2] ) ).toBe( 0.01 ); } ); } ); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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.
 

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