Monday, November 2, 2015

Bonfire: Diff Two Arrays

Cracked this pretty easily i mean i started yesterday evening and was done today morning.
I used the consoler to debug using the last test case.

[1, "calf", 3, "piglet"], [7, "filly"] should return [1, "calf", 3, "piglet", 7, "filly"].


Here is my solution.
function diff(arr1, arr2) { var newArr = []; // Same, same; but different. var newarr2 = []; var newarr1 = []; for(i=0;i<arr2.length;i++) { if(arr1.indexOf(arr2[i])=== -1) { newarr1.push(arr2[i]); } console.log(newarr1); } for(i=0;i<arr1.length;i++){ if(arr2.indexOf(arr1[i])=== -1){ newarr2.push(arr1[i]); } console.log(newarr2); } var newarr = newarr1.concat(newarr2); return newarr; } diff([1, 2, 3, 5], [1, 2, 3, 4, 5]);

Bonfire:Sum of all Numbers in a Range.

I skipped directly to the Bonfires, since for Ziplines i think i need to brush up a little more on Bootstrap and HTML, CSS.
Planning to do the codeacademy tracks and become little more confident before i do the Ziplines.

This is my solution for sum of all numbers in a range.

Math.min() and Math.max() did not work when i passed the arr.
So googled it a bit and then passed elements of the array.

The sum of numbers in a range using Array.reduce() was already given in MDN example.
Solution:

function sumAll(arr) {
  var newarray = [];
  var minofarray = Math.min(arr[0],arr[1]);
  var maxofarray = Math.max(arr[0],arr[1]);
  for(i=minofarray;i<=maxofarray;i++){
    newarray.push(i);
    
  }
  var sumofarr = newarray.reduce(function(a, b) {
  return a + b;
});
  return sumofarr;
  
}

sumAll([1, 4]);