Ph Dev Ph Dev - 4 years ago 150
Javascript Question

Compare Numbers using Javascript

I just want to compare right numbers and left numbers using javascript, this is my input sample

290|0
290|0
290|2902
250|0
250|0
0|0
0|1299.95
0|1299.95
250|0
290|0
290|0
290|22
32|1299.95
0|0


then the results are greater numbers:

290
290
2902
250
250
0
1299.95
1299.95
250
290
290
290
1299.95
0


Please help!

Answer Source

Here's how to do it the long way:-

var input = '290|0\n\
290|0\n\
290|2902\n\
250|0\n\
250|0\n\
0|0\n\
0|1299.95\n\
0|1299.95\n\
250|0\n\
290|0\n\
290|0\n\
290|22\n\
32|1299.95\n\
0|0'

var rows = input.split( '\n' )
var output = [ ]

for (var i = 0; i < rows.length; ++i) {
    var row = rows[i]
    var parts = row.split( '|' )
    if ( 1*parts[0] > 1*parts[1] ) 
        output.push( parts[0] )
    else
        output.push( parts[1] )
}
output = '\n'.join( output )
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download