Saturday, May 27, 2017

JavaScript - A simple demo of NaN

var a = NaN;
console.log(a); //NaN
console.log(typeof(a)); //Number

What will be the outcome of below code?
if(a <7 || a > 7 || a==7 || NaN==NaN || a==NaN){
 console.log(yes)
}else{
 console.log(no)
}

What? JavaScript?

JavaScript is a Class-Less language. So, JS Developers also classless?

JavaScript is like a Religion, people use it or misuse it, as per their convenience.

JS is not object oriented language. JS does not have class but everything is an object. JS does not have class but object is an instance of classs. So funny!

JS is a Class-Free language. JS needs an object to create an object. Then, who created first object? From where did it come?

How to create a new cryptocurrency?

There are always many different approaches and methods to do the same thing. Explore what works best for you.

  • Choose a platform for starting your cryptocurrency: 
    • Ethereum: Etherem allows the most advancements.
    • Bitcoin fork: Bitcoin fork is easiest.
    • Ripple: Ripple is private. You cannot start your own over it. You will stay a minority stake holder.
    • Nxt: Nxt is private. You cannot start your own over it. You will stay a minority stake holder.
  • Link the price to an external asset but do not back it by that asset. For example KARMA is backed by real karma in life. 
  • Provide the ability to give away some of the currency without incurring costs other than admin costs.

Ethereum has the largest development community of all the cryptocurrencies. It is considered to be most advanced in terms of options it provides. It has best resources for you to improve your cryptocurrency and experiment. To start with Ethereum,

  • Download the Mist wallet
  • Follow the instructions. It gives you detailed instructions on how to launch your own cryptocurrency.


Define your crytocurrency with its features. KARMA has been used as an example.


  • Symbol: KARMA
  • Name: Karma Coin
  • Description: Token of Thanks in Appreciation of Good Karma
  • Platform: Ethereum. Ethereum is chosen here because KARMA is mainly about contracts with other people.  
  • Algorithmhttp://www.coinwarz.com/cryptocurrency 
  • Block time: 1 min. This is the average target time between blocks. Usually it is between 15 sec and 2 mins. Bitcoin has 10 minutes.
  • Difficulty retarget time: 1 hour. Usually it is between 1 hour to 1 day. [(diff retarget time)/(block time)] gives you how many blocks a diff retarget will happen. This is an important parameter to consider.
  • Initial coin per block: 100. Usually it is 2 to 100. You can also do coins per block based on the block number, or even randomly (like JKC/LKY etc).
  • Coins per block halving time: 1 year. Usually it is 6 month to 3 years. You may choose as you wish. You may also want to think whether you really need it?
  • Ports, you need two: Choose the ones that are not used in common apps. You can google for a particular port usage.
  • Official website: Website
  • Block Explorer: Block Explorer Site

Monday, June 20, 2016

Moodle Interview Questions

Q. What is a Moodle course?
A. A Moodle Course is a place where teacher delivers content and assesses students. Teacher gives knowledge to students using activities and resources and assess them using assignments and quizes.

Q. What structure comes to mind  when observing the relationship of Moodle categories and courses?
A. Categories are similar to folders and courses are similar to files. Like folders, categories can have sub-categories but courses cannot have sub-courses. Subjects map to courses.

Q. Where exactly do you put the content - the more specific you are the better?
A. Content is put inside activities and resources.

Q. Which feature(s) of Moodle allow us to enrol students before we have fully created the content?
A. Hiding the content from students before it gets fully created.

Q. Is it possible to change a course format?
A. Yes, it is possible to change a course format.

Q. What does CC stand for?
A. Course Category

Sunday, May 29, 2016

Creating, Signing, Zipaligning an APK from a cordova project for Android

1. Create the project.

Create the project and write all your code here.
cordova create appname com.domain.appname appname

2. Add the platform.

cd appname
cordova platform add android

3. Build an unsigned APK 

which can be uploaded to emulators or devices via ADB or similar for initial testing.
cordova build android

4. Create keystore for signing. 

Navigate to platforms/android from the root of your project. Don't forget the password entered below, otherwise you will no longer be able to update your application in the play store. Use below command to create a new keystore and certificate, the alias used is generally the name of your application.
keytool -genkey -v -keystore appname.keystore -alias appname -keyalg RSA -keysize 2048 -validity 100000

5. Build unsigned release APK.

cordova build android --release

6. Sign the jar file. 

You may get a message, "'jarsigner'/'keytool' is not recognized as an internal or external command, operable program or batch file: Looks like JDK isn't installed, or is installed but is not defined in your environment variables." In that case, download and install JDK 6; such as JDK 6u45, it is ok as long as it is not JDK 7. Google Play Store doesn't play well with JDK 7, and the easiest thing to do right now is keep true with JDK 6. You'd think the problems with signing in JDK 7 would be fixed in JDK 8. That IS NOT the case. Please use JDK 6.
jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore <PATH TO KEYSTORE FILE> <PATH TO CORDOVA BUILT APK> <KEYSTORE ALIAS>
jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore appname.keystore build/outputs/apk/android-release-unsigned.apk appname

7. Zipalign the app to make it ready for publication. 

If you get the message, "zipalign' is not recognized as an internal or external command, operable program or batch file," looks like the ADT Bundle isn't installed, or is installed but is not defined in your environment variables. Install ADT if not done already and copy zipalign.exe to the current folder. zipalign must only be performed after the .apk file has been signed with your private key.
zipalign -v 4 PATH/TO/YOUR_SIGNED_PROJECT.apk PATH/TO/YOUR_SIGNED_AND_ALIGNED_PROJECT.apk
zipalign -v 4 build/outputs/apk/android-release-unsigned.apk build/outputs/apk/android-release-signed-aligned.apk

Test before uploading to the play store.

Its best to test the new APK in real devices once it has been connected via USB. First remove any previous development versions of your application from the device, otherwise you will receive a certificate error upon installation of the new signed APK. Next, install the new signed APK to the device by uploading it with the following command:
adb install build/outputs/apk/android-release-signed-aligned.apk

Tuesday, May 10, 2016

Browser vs App

Browser:

  • Pros
    • Comes preinstalled on iPhone
    • Comparatively less effort to include different mobile devices 
  • Cons
    • Less performance friendly
    • Less screen space

App:

  • Pros
    • Performance improvement due to device specific resources
    • Native touch and sensor support 
    • Can leverage more screen real state as compared to browser
  • Cons
    • Additional development and maintenance effort

Wednesday, November 25, 2015

JavaScript Best Practices

Good practices:


  • If something is obvious but taking time to implement, innovate. Find ways to do it simply instead of hard way. If you are frustrated there is problem with code or with you or with boss or with environment. Fix it.
  • Create functions with single object parameter. Object parameter can have multiple properties. Then, the user of your function does not need to worry about order and number of parameters.
  • Use debug = true/false to load dev vs minified files.
  • Use debug = true/false to load files from production or from local server.
  • Use event bus (pub/sub pattern). It provides Loose Coupling.
  • Declare variables, on top. Then, use.

Worst practices to avoid:


  • Avoid fear to touch, modify, replace your own code later.
  • Do not rely too much on magic of frameworks doing workarounds around the black boxes such as using timeinterval
  • Aoid unpredictable code which may make you unsure that changes may impact unknown parts of your application
  • Using jugad for the time being to make the code work.
  • No fine control - by using your framework, tools automating in your code
  • Remove code duplicaiton. Do not repeat same code and logic at many places.
  • Not refactoring your code.
  • Code sticked to DOM structure does not allow to move the component from one page/place to another.
  • Sometimes things are obvious to implement but restricted by architecture/framework/library.
  • Do not have global variables except if absolutely necessary.


Tuesday, November 10, 2015

JavaScript Interview Questions

When should you use var keyword in declaration and when should you not?
As a good practice, one should always use the var keyword. Not using var, creates a global variable. So, we should avoid avoiding var.
(function() {
   num1 = 1;
   var num2 = 2;
})();
console.log(num1); // outputs 1
console.log(num2); // error: num2 is not defined

What is prototypal inheritance in JavaScript?
JavaScript (ECMA5) does not have classes and does not support classical inheritance. Instead, each object can be a prototype of another object. Then, the object inherits the properties of its prototype. In below example, properties of animal are inherited by cow and the properties of cow are inherited by calf. So, calf has four legs same as cow and animal.
var animal = {legs:4};
var cow = Object.create(animal);
var calf = Object.create(cow);

Why would you include 'use strict' at the beginning of a JavaScript source file?
Using strict mode enforces stricter error checking at run time. It raises errors that would have otherwise failed silently. It helps to avoid simple mistakes like creating accidental globals, undefined values, or duplicate property names.

What datatypes are supported in JavaScript?
Number, String, Boolean, Object, Function, Null, Undefined

What is the difference between == and === ?
‘==’ evaluates equality of the value, while ‘===’ evaluates  equality of both type and value.

What is the  difference between window.onload() and the jQuery $(document).ready() method?
The window.onload method runs after all the assets (HTML, CSS, images) have loaded. The $(document).ready() runs as soon as the Document Object Model (DOM) is loaded.

How do you check if a variable is an object?
null is an object but null object is 'falsy'. So this works -
if(foo && typeof foo === "object") {
console.log('foo is object and is not null');
}

What are closures in JavaScript and how are they used?
A closure is an expression that can have free variables together with an environment that binds those variables.

What is the difference between Function as an expression and Function as a statement?
Function as an expression. Cannot be used before it is created. var add = function do(){};
Function as a statement. function do(){}; Can be called before it is created because it is pushed to top of scope.

Does JS need polymorphism?
JS parameters is English word. Arguments is a keyword. When parameters are passed to a function. JS passes one more parameter to the function called arguments. If you do console.log(arguments.length), it gives number of all the arguments passed. arguments is an array LIKE object. To fix max number of parameters inside function check arguments.length <= 1. arguments.push() does not work as arguments is not an array.

If you pass less number of parameters, JS does not complain.

If function does not have return statement, it returns undefined.

What are the different ways to create an object?
You can create and object in three possible ways -
- Object as a literal: var x = {a:'3', b:'4'};
- Object using a constructor
- Using Object.create

Whatever way and as soon as you create a function, always its prototype object gets created. In JS, an object creates another object. So, when you create a function, constructor in JS is not a concept, its just a function.

Object as a literal:
No matter how you create object with properties.JavaScript properties of an object are acually stored as an object. It works as an array of objects. To see the object run below code.
var x = {a:'3', b:'4'};
console.log(Object.getOwnPropertyDescriptor(x, 'a'));

//Make a property readonly
var x = {a:'3', b:'4'};
Object.defineProperty(x,'a', {writable:false});
console.log(Object.getOwnPropertyDescriptor(x, 'a'));

//To generate error if someone tries to overwrite this, write 'use strict'
'use strict'
var x = {a:'3', b:'4'};
Object.defineProperty(x,'a', {writable:false});
console.log(Object.getOwnPropertyDescriptor(x, 'a'));

//Make a json object
var x = {a:'3', b:'4'};
console.log(JSON.stringify(x));

//Make one property enumerable false. It does not show up in console. The property will be available by dot operator x.a but it cannot be enumerated in for loop. Using for loop it will not be available.
var x = {a:'3', b:'4'};
Object.defineProperty(x,'a', {enumerable:false});
console.log(JSON.stringify(x));
 
//Object using a constructor
function foo(who){
this.me = who;
}
var a1 = new foo("james");
var a2 = new foo("bond");
console.log(a1.me);
console.log(a2.me);
 //a1.[[Prototype]] gives foo.prototype.
 //As [[Prototype]] is hidden, Mozilla introduced __proto__
 //So, function has prototype, object has __proto__
 //In IE, __proto__ does not work so use, a1.contructor.prototype
 
 

Tuesday, September 15, 2015

Principles of Programming

S.O.L.I.D. Principles


  • SRP: The Single Responsibility Principle(SRP) states that each class, module in application should change only for a single reason.
  • OCP: Open/Closed Principle (OCP) states that software entities (classes, modules, functions, etc.)  should be open for extension but closed for modification. A class's behaviour should be extendible without modifying its behaviour.
  • LSP: Liskov Substitution Principle states that  Derived types must be substitutable for their base types.
  • ISP: Interface Segregation Principle (ISP) states that Clients should not be forced to depend upon interfaces  that they don’t use. 
  • DIP: Dependency Inversion Principle(DIP) states that a. Abstraction - High level modules should not depend upon low level modules, both should depend upon abstractions. b. Ownership - Low level implementation should depend on high level concepts.