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


Overview of React


  • Is a JavaScript framework for building User interface
  • It is not a framework
  • It is considered as an view in MVC
  • Virtual DOM gives React it's edge and makes it fast
Virtual DOM:
  • Uses a virtual DOM to push only changes to the DOM instead of re-rendering everything
  • V-Dom allows  rendering on the server side and sending the final HTML to the browser (SEO friendly )
JSX;
      Allows html in JavaScript which results in self contained components

Local storage in the browser


  • The read-only local-storage property allows you to access a Storage object for the Document's origin;
  • The stored data is saved across browser sessions. 
  • local-storage is similar to session-storage, except that while data stored in local-storage has no expiration time, data stored in session-storage gets cleared when the page session ends — that is, when the page is closed.

Classifying data into predefined categories


Input and output for classification problem


  • Input to classification problem is a feature and output is called as label
  • Problem statement and training data is where we spend amount of time

Lets talk about 2 types of problems

  Problem statement 1
     Email, tweet or trading day
  • Types of problems are Spam or Ham
  • Tweet positive or negative
  • Trading day up-day or down-day

ngx-bootstrap in angular 2


Create a new project
ng new wakeGenie

Create a new component
C:\Development\Wakegenie\webApp>ng generate component modal

Install bootstrap in angular 2

Use of Export and Require


Export
  • The export statement is used when creating JavaScript modules to export functions, objects, or primitive values from the module so they can be used by other programs with the import statement
  • Along with export we also need require or import
Require

  •  Used to consume modules. It allows you to include modules into your programs. You can include built-in core Node.js modules, community-based modules (node_modules) and local modules

String to JSON object

  •  A common use of JSON is to exchange data to/from a web server.
  • When receiving data from a web server, the data is always a string.
  • Parse the data with JSON.parse(), and the data becomes a JavaScript object.
Edit in plnkr

We would usually receive text from the server. We need to convert it into JavaScript object. In this scenario we use JSON.parse() function.

console.log("** Convert string into JSON object *****")
  
  var jsonstring='{"firstName":"Prathap","lastName":"Kudupu"}'
  
  console.log("\n string-->",jsonstring);
 
  var obj=JSON.parse(jsonstring);
  
  console.log("\nJSON-->",obj);

Multiple panels with css



In this sample we are using bootstrap panel and modified css. This can be used as a standard panel in your website


JSON object to string

 
  •        A common use of JSON is to exchange data to/from a web server
  •       When sending data to a web server, the data has to be a string
  •       Convert a JavaScript object into a string with JSON.stringify()
Edit in plkr

 console.log("*** Convert JSON object to a string");
  var obj={
    firstName:"Prathap",
    lastName:"Kudupu"
  }
  console.log('\n obj', obj)
  console.log('Convert JSON object to string',JSON.stringify(obj));

Output

Note: The difference between string and object is that the property name is the object is not a string


Split and join in JavaScript


Split a phrase into words
This function takes a given phrase and splits based on space into a new array
ex : It would take "Prathap  Kudupu"  it would add it to an array with prathap and kudupu

Edit in plnkr
 console.clear();
  console.log("**** Split a phrase *****");
  
  function splitPhrase(str){
    console.log(str);
    return str.split(' ');
  }
  console.log(splitPhrase('Prathap Kudupu'));
  

Transcript vs caption


Captions 

  • Appear on screen simultaneously with the audio and video, and follows the same timing. 
  • It exists within the video player, and generally speaking, can’t be referenced outside of the video. 
Transcript
  • A transcript is the same word-for-word content as captions, but presented in a separate document, whether it is a text file, word processing document, PDF, or web page. 
  • The transcript of a video could easily be generated from a script, if the video was scripted before production. If the video was not scripted, then the same process of transcribing the audio from the video into typed words is required.

Difference between let and var



The difference is scoping. var is scoped to the nearest function block and let is scoped to the nearest enclosing block, which can be smaller than a function block. Both are global if outside any block.
Also, variables declared with let are not accessible before they are declared in their enclosing block. As seen in the demo, this will throw a ReferenceError exception.
They are very similar when used like this outside a function block.
let me = 'go';  // globally scoped
var i = 'able'; // globally scoped
However, global variables defined with let will not be added as properties on the global windowobject like those defined with var.
console.log(window.me); // undefined
console.log(window.i); // 'able'

Tuesday, August 8, 2017

All about angular CLI



BEFORE YOU INSTALL: please read the prerequisites
npm install -g @angular/cli

Usage

ng help

Using bootstrap in angular 2

To use bootstrap in angular 2  we need to add ngx-bootstrap



  • install ngx-bootstrap and bootstrap
  npm install ngx-bootstrap bootstrap --save
  • open src/app/app.module.ts and add

Add Extensions for visual studio code

We can add extensions to visual studio code using


Example:
If we want to use TSLint (extensible static analysis tool that checks TypeScript code for