Thursday, August 31, 2017

Java script cheat sheet



Push to an array

 outArray.push(arr2[k].charCodeAt()-64)
Convert number to character using ASCII

 outArray.push(arr2[k].charCodeAt()-64)
Convert character to number using ascii

/convert the character value to number
           //note we are subtracting 64
              outArray.push(arr2[k].charCodeAt()-64)

Get first character from a string
function validatePalindrome(word)
{
   //charArray = word.split;
   return word[0];
}
//Function call 
console.log(validatePalindrome("BOB"));




Loop through a word

function validatePalindrome(word)
{
   var len=word.length;
   var reverse="";
   for(i=0;i<=len;i++)
   {
       reverse=reverse+word[i];
       console.log(reverse);
     
   }
   
}
//Function call 
console.log(validatePalindrome("BOB"));

Reverse traverse an array


function revIntArrayWihoutTemp(arr)
{
   console.log(arr);
   var len=arr.length;
   for(i=len-1;i>=0;i--)
   {
       console.log(arr[i]);
   }
}
//function call
console.log(revIntArrayWihoutTemp([1,5,8,10]));

Java Script Split and Join

/**
 * Demonostrate the use of split and join
 */
function splitWord(str)
{
    console.log(str);
    return str.split('');
}

function joinWord(str)
{
    
    str=str.split('');
    console.log(str);
    return str.join('');
}

//Split splitWorld
console.log(splitWord("Prathap Kudupu"));
console.log(joinWord("Prathap Kudupu"));
Output



Java Script contains

var str = "Hello world, welcome to the universe.";
var n = str.includes("world");

Compare two strings in JavaScript

 if (a.trim()==b.trim()) return -1;
IsAlphaNumeric

function isAlphaNumeric(input)
{
    var char=/^[0-9a-zA-Z]+$/;

   return (input.match(char))?true:false;
}

No comments:

Post a Comment