javascript - slice method is not working as expected -


i trying solve problem (https://www.hackerrank.com/challenges/service-lane). write code

'use strict';  function processdata(input) {     var lines = input.split('\n');     var nfreelane = lines[0].split(' ')[0];     var ntestcase = lines[0].split(' ')[1];     nfreelane = parseint(nfreelane);     ntestcase = parseint(ntestcase);     var nfreewidth = lines[1].split(' ').map(function(num) {         return parseint(num);     });     (var = 2; < ntestcase + 2; i++) {         var xx = lines[i].split(' ');         var entrypoint = xx[0];         var exitpoint = xx[1];         var nar;         if (exitpoint == nfreewidth.length - 1) {             nar = nfreewidth.slice(entrypoint);         } else {             console.log('coming here');             console.log('exit point is' + exitpoint);             console.log(nfreewidth.length);             nar = nfreewidth.slice(entrypoint, exitpoint + 1);             console.log('nar is' + nar);         }         if (math.min.apply(null, nar) >= 3) process.stdout.write('3\n');         if (math.min.apply(null, nar) == 2) process.stdout.write('2\n');         if (math.min.apply(null, nar) == 1) process.stdout.write('1\n');     } } process.stdin.resume(); process.stdin.setencoding("ascii"); var _input = ""; process.stdin.on("data", function(input) {     _input += input; }); process.stdin.on("end", function() {     processdata(_input); }); 

when run code input

5 5     1 2 2 2 1 2 3 1 4 2 4 2 4 2 3 

expected output

2 1 1 1 2 

but output is

1 1 1 1 1 

when use slice method

if (exitpoint == nfreewidth.length - 1) {     nar = nfreewidth.slice(entrypoint); } else {     console.log('coming here');     console.log('exit point is' + exitpoint);     console.log(nfreewidth.length);     nar = nfreewidth.slice(entrypoint, exitpoint + 1);     console.log('nar is' + nar); } 

nar giving 2,3 [2,2,1] expected [2,2] .can 1 please tell why showing behaviour.please explain.thanks

your

var exitpoint = xx[1]; 

is string. when exitpoint + 1 in

nar = nfreewidth.slice(entrypoint, exitpoint + 1); 

you doing string concatenation: "2"+1 == "21". slice accept (convert number) gladly , return array order of magnitude long…


here's fixed, simplified , working version of code:

function processdata(input) {     var lines = input.split('\n');     lines.shift(); // uninteresting stats, might simplify parsing in other languages     var widths = lines.shift().split(' ').map(number);     // console.log("passed widths: ", widths.length);     (var = 0; < lines.length; i++) {         var parts = lines[i].split(' ');         var entrypoint = +parts[0]; // "+" casts number         var exitpoint = +parts[1];         // console.log('in: '+entrypoint+', out: ' + exitpoint);         var sectionwidths = widths.slice(entrypoint, exitpoint + 1);         // console.log('sections ' + sectionwidths);         var minwidth = math.min.apply(null, sectionwidths);         process.stdout.write(math.min(minwidth, 3)+'\n');     } } 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -