Handle NaN
f=a=> a.sort()[0]!==a[1] || isNaN(a[0])!==isNaN(a[1])? a[0] : a[0]===(a=a.pop()) ? 'Error' : a
f=a=>a.sort()[0]!=a[1]?a[0]:a[0]==(a=a.pop())?'Error':a- f=a=>
- a.sort()[0]!==a[1] || isNaN(a[0])!==isNaN(a[1])?
- a[0] :
- a[0]===(a=a.pop()) ?
- 'Error' :
- a
describe("Solution", function() { it("simple", function() { Test.assertEquals(f([1,1,4]),4); Test.assertEquals(f([1,4,1]),4); Test.assertEquals(f([4,1,1]),4); Test.assertEquals(f([1,1,4,1,1]),4); Test.assertEquals(f([1,1,1,4,1]),4); Test.assertEquals(f([1,1,1,1,4]),4); Test.assertEquals(f(['AD','Ad','Ad']),'AD'); Test.assertEquals(f(['Ad','AD','Ad']),'AD'); Test.assertEquals(f(['Ad','Ad','AD']),'AD'); Test.assertEquals(f(['null','nulll','null']),'nulll'); }); it("type checking", function() { Test.assertEquals(f([false,null,null]),false); Test.assertEquals(f(['',null,'']), null); Test.assertEquals(f(['0','0',0]), 0); Test.assertEquals(f([0,'','']), 0); Test.assertEquals(f([null,0,null]), 0); Test.assertEquals(f([undefined,undefined,0]),0); }); it("wrong", function() { Test.assertEquals(f([undefined,undefined,undefined,undefined]),'Error'); Test.assertEquals(f([null,null,null,null]),'Error'); }); it("NaN", function() { // assertEquals doesn't handle NaN Test.assertSimilar(f([NaN,null,null]), NaN); Test.assertSimilar(f([NaN,null,NaN]), null); Test.assertSimilar(f([NaN,NaN,null]), null); Test.assertSimilar(f([NaN,NaN,false]),false); Test.assertSimilar(f([NaN,NaN,NaN,NaN]),'Error'); }); });
- describe("Solution", function() {
- it("simple", function() {
- Test.assertEquals(f([1,1,4]),4);
- Test.assertEquals(f([1,4,1]),4);
- Test.assertEquals(f([4,1,1]),4);
- Test.assertEquals(f([1,1,4,1,1]),4);
- Test.assertEquals(f([1,1,1,4,1]),4);
- Test.assertEquals(f([1,1,1,1,4]),4);
- Test.assertEquals(f(['AD','Ad','Ad']),'AD');
- Test.assertEquals(f(['Ad','AD','Ad']),'AD');
- Test.assertEquals(f(['Ad','Ad','AD']),'AD');
Test.assertSimilar(f(['null','nulll','null']),'nulll');- Test.assertEquals(f(['null','nulll','null']),'nulll');
- });
- it("type checking", function() {
- Test.assertEquals(f([false,null,null]),false);
- Test.assertEquals(f(['',null,'']), null);
- Test.assertEquals(f(['0','0',0]), 0);
- Test.assertEquals(f([0,'','']), 0);
- Test.assertEquals(f([null,0,null]), 0);
- Test.assertEquals(f([undefined,undefined,0]),0);
- });
- it("wrong", function() {
- Test.assertEquals(f([undefined,undefined,undefined,undefined]),'Error');
- Test.assertEquals(f([null,null,null,null]),'Error');
- });
- it("NaN", function() {
- // assertEquals doesn't handle NaN
- Test.assertSimilar(f([NaN,null,null]), NaN);
- Test.assertSimilar(f([NaN,null,NaN]), null);
- Test.assertSimilar(f([NaN,NaN,null]), null);
- Test.assertSimilar(f([NaN,NaN,false]),false);
- Test.assertSimilar(f([NaN,NaN,NaN,NaN]),'Error');
- });
- });