My First Playground

node v8.16.1
version: 1.0.0
endpointsharetweet
This is a playground to test JavaScript. It runs a completely standard copy of Node.js on a virtual server created just for you. Every one of npm’s 300,000+ packages are pre-installed, so try it out:
function secondMax(arr){ let first, second; if(!Array.isArray(arr)){ return -1; } for(val of arr){ if(parseFloat(val) % 1 !== 0){ return -1; } } if(arr.length<2){ return -1; } else if(arr.length==2){ if(arr[0]>arr[1]){ return arr[1]; } else if(arr[0]==arr[1]){ return -1; } else { return arr[0]; } } for(i=0;i<arr.length;i++){ if(first==undefined){ first = parseInt(arr[0]); continue; } if(second==undefined){ if(arr[i]<first){ second = parseInt(arr[i]); } else { second = first; first = parseInt(arr[i]); } continue; } if(parseInt(arr[i])>first){ second = first; first = parseInt(arr[i]); } else if(parseInt(arr[i])<first && (parseInt(arr[i])>=second || first===second)){ second = parseInt(arr[i]); } } if(first==second){ return -1; } return second; } console.log(["3","2","1.1"], secondMax(["3","2","1.1"])); console.log(["3","3","-3","2","-2"], secondMax(["3","3","-3","2","-2"])); console.log(["3","3","-3"], secondMax(["3","3","-3"])); console.log(["3","3"], secondMax(["3","3"])); console.log(["3","3","3"], secondMax(["3","3","3"])); console.log(["3"], secondMax(["3"])); console.log(["3","-4","-3"], secondMax(["3","-4","-3"])); console.log(["3","2","ok"], secondMax(["3","2","ok"])); // infinity console.log('infinity number', secondMax([ "1234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891111", "1234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567892122", "-2" ])); console.log(secondMax([ "1234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891", "1234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567892", ]));
Loading…

no comments

    sign in to comment