I want to cast a string to number

hi,
My challenge doesn’t work because the cast:

function diffArray(arr1,arr2) {
  
  var arr=[],
      out=[],
      counts={};

  //concat the two array
  arr = arr1.concat(arr2);
  for (var i=0, len=arr.length;i<len;i++) {
    var item = arr[i];
    //insert each item in the properties object counts
    counts[item] = (counts[item] >= 1) ? counts[item] + 1 : 1;
  }

  for (var key in counts) {
    //loop in the object
    if (counts[key] === 1) {
      //create an array with all the value with 1
      out.push(key);
    }
  }

  console.log(out);
  // loop the final tab to cast the number in the string to a number
  for (var j = 0, lon = out.length; j < lon; j++) {
    var num = out[j];
    if (!isNaN(num)) {
     console.log(num);
      parseInt(num);
      //console.log(parseInt(counts[key]) + ': ' +key);
    }
    
  }
  console.log(out);
  return out;
}

diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);
is return [“4”] and it expect [4]

Thanks

I found the solution with another array…
Thanks anyway !!!

with underscore.js the challenge is done in one line :smile:

 .uniq([1, 2, 1, 4, 1, 3]);
=> [1, 2, 4, 3]

INSANE

1 Like