Friday, June 10, 2016

Truncate a String

//My solution for this
function truncateString(str, num) {
  // Clear out that junk in your trunk
  //var newstr = '';
  if(num <= 3){
    str = str.slice(0,num) + '...';
  }
  else if(str.length > num){
    str = str.slice(0,num-3) + '...';
  }

  return str;
}

truncateString("A-tisket a-tasket A green and yellow basket", 11);

Its quite simple but was getting stuck. Finally 

Friday, June 3, 2016

Confirm the Ending--My solution

/* var test = "He has to give me a new name";


var joinedarray = "He has to give me a new name".split(" ").join("").lastIndexOf("name");
var t = "He has to give me a new name".split(" ").join("").length - "name".length;
console.log(joinedarray);
console.log(t); */


function confirmEnding(str, target) {
  // "Never give up and good luck will find you."
  // -- Falcor
 
  var joinedarrayindex = str.split(" ").join("").lastIndexOf(target);
  var test = str.split(" ").join("").length - target.length;
   if(test == joinedarrayindex){
    return true;
   }
   else{
    return false;
   }
  //return str;
}

confirmEnding("If you want to save our world, you must hurry. We dont know how much longer we can withstand the nothing", "mountain");

Thursday, June 2, 2016

Return Largest Numbers in Arrays

Taking this step by step.

First to get the largest number in an array

function largestOfFour(arr) {
  // You can do this!
  for(i=0;i<arr.length;i++){
  var largest = 0;
  if(arr[i]>largest){
  largest = arr[i];
  }
  return largest;
  }
  return arr;
}

largestOfFour([4, 5, 1, 3]);


Second Step:

function largestOfFour(arr) {
  // You can do this!
   for(i =0 ; i <arr[i].length; i++){

//The trick is to assign the largest variable correctly.
     var largest = 0;
     for(k=0;k<4;k++){
       if(arr[i][k] > largest){
        largest = arr[i][k];
       }
     }
    console.log(largest);
   }
  return arr;
}

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);


Finally


function largestOfFour(arr) {
  // You can do this!
   var newarray = [];

  for (var i = 0; i < arr.length; i++){

     var largest = [];
     for(k=0;k<4;k++){
       if(arr[i][k] > largest){
        largest = arr[i][k];
       }
     }
     newarray[i] = largest;
     }
 return newarray;
}

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);


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]);

Wednesday, October 21, 2015

Bonfire : Check for Palindrome.

They mention here that we should remove all punctuation marks in the string.

Here are the strings  in the Bonfire test cases with punctuation marks.
"A man, a plan, a canal. Panama"
"My age is 0, 0 si ega ym."
"0_0 (: /-\ :) 0-0"


Used little bit of stackoverflow and MDN to understand .replace()
This is what I got:

Used a lot of console to debug each string and that really helped.



Code starts here

function palindrome(str) {
  
  // Good luck!
  //Removing punctuation marks and spaces in original string..
  var originalstring = str.replace(/[.,-\/#!$%\^&\*;:{}=\-_`~() " "]/gi,"").toLowerCase();
  var reversedstring = str.replace(/[.,-\/#!$%\^&\*;:{}=\-_`~() " "]/gi,"").toLowerCase().split("").reverse().join('');
  if(reversedstring === originalstring)
    {
  return true;
    }
  else
    {
      return false;
    }
}




palindrome("eye");






Factorial of Number Bonfire Solution

Used a while loop for this and it worked ok.

function factorialize(num) {
  factorial = 1;
  i = 1;
  while(i<=num)
    {
      factorial = factorial * i;
      i++;
    }
  return factorial;
}

factorialize(5);