JS: storing function inside array and calling that function

We can store function inside the array and call it.

Ex.:

var arr = []; // note this creates array in js

var obk = {} // this creates empty ojbect

Ex.2:

var arr = [ “1”,

2,

false,

{ name = “James”,

address = “Mulholland Drive”

},

function(name){

var greeting = “hello “;

console.log(greeting+name);

}

]

Now we have different variables inside our array, we even have another array and function!

To call the stored function:

Note: to call the method use arr[4]()

arr[4](arr[3].name);  // this will get the name from the array under index 3 and pass it to the function

// output will be: hello James

JS function parameters: arguments and Spread

Unlike Java, JS will not throw any errors if the way function is declared and called is different. So there are 2 scenarios when developer passes wrong number of parameters. Either too many or too less

If too many:

Ex: function xyz(x, y,z){

console.log(x);

}

xyz();

// output will be undefined

So, to tackle this potential issue when other developers pass more arguments to a method than it can accept we can use either arguments or spread.

Ex

function abc(a, b,c){

console.log(arguments);

}

// arguments is keyword and will return a,b,c or whatever arguments passed to abc function.

Works a bit similar to this keyword can access it through arguments[n]

Another way is using spread:

Ex.:

function restParam(parameter1, …restArgs){
console.log(restArgs.length); // Logs the number of arguments that do not have a corresponding parameter
console.log(restArgs[2]); // Logs the 3rd argument after the number of arguments that do not have a corresponding parameter
}

restParam(1,2,3,4,5);
// Log would be as follows
// 4
// 4

// 4 is the number of arguments that do not have a corresponding parameter
// 4 is the 4th 3rd argument after the number of arguments that do not have a corresponding parameter

Second way is preferred, because arguments only return array like collection. The keyword is “like”, so some array methods won’t work. (arguments.length and arguments[n] will still work )

Oh, yeah. In the case if there are less parameters passed

If too less:

just assign the functions parameters to a default variable:

fuction zzz(a, b,c){

a= a || “a”;

b=b || 7;

c = c || 10;

}

How to: Missing commit (error: unpack failed: error Missing commit) on Smargit

Go to the folder where your project is stored, then open the Git bash and run below commands:

  1. git gc
  2. git pull –rebase
  3. git push

In case you have this error:

error: cannot pull with rebase: You have unstaged changes.
error: please commit or stash them.

Then, just commit the files first. You can do it in the smart git.

 

Python: Revere string

Probably the easiest and close to the fastest way to reverse a string is to use Python’s extended slice syntax. This allows you to specify a start, stop and step value to use when creating a slice. The syntax is: [start:stop:step].

s = "String to reverse."
print s[::-1]

If start is omitted it defaults to 0 and if stop is omitted it defaults to the length of the string. A step of -1 tells Python to start counting by 1 from the stop until it reaches the start.

When working with large strings, or when you just don’t want to reverse the whole string at once, you can use the reversed() built-in. reversed() returns an iterator and is arguably the most Pythonic way to reverse a string.

 

s = "String to reverse."
print ("".join(reversed(s)))

Java utils collections: Map

Difference between HashMap, LinkedHashMap and TreeMap

  • HashMap makes absolutely no guarantees about the iteration order. It can (and will) even change completely when new elements are added.
  • TreeMap will iterate according to the “natural ordering” of the keys according to their compareTo() method (or an externally supplied Comparator). Additionally, it implements the SortedMap interface, which contains methods that depend on this sort order.
  • LinkedHashMap will iterate in the order in which the entries were put into the map

hashtables.JPG

taken from:

https://stackoverflow.com/questions/2889777/difference-between-hashmap-linkedhashmap-and-treemap

Check the tomcat app.log in case of unexpected error

If you encounter some unexpected error with your web app, the best place to check what’s wrong is Tomcat’s app.log file.

1. Firstly, ssh to the server containing your app:

$ssh -A xxx.dev.XXX ( you can set shortcuts like in this file  ~/.ssh/config)

2. Get the root access

$sudo su –

3. Get the tail of the log. Tail command is useful to see the latest log entries and -f helps to keep refreshing it.

$tail -f app.log

Then replicate the bug, you should see the error message there. It can be null pointer exception for instance.

JavaScript, why are you like this?

JavaScript have some weird quirks. And one of them is how pointer “this” works.

look at the code below:

[code language=”javascript”]
var c = {
name: "this is c object",
log : function(){
this.name = "updated c object";
var setName = function(newname){
this.name = newname;
}
setName("Updating again! The c object");
console.log(this);
}
}
[/code]

Look at the line 6, you might think that name has been set to value “Updating again! The c object”, because this is pointing to c object. BUT, if you run the code, you will find out that actually line 6, is creating new Global variable name, and setting it’s value to “Updating again! The c object”. This happens because of how the execution context is created in JavaScript. In JS, the interpreter will hoist all the variables and methods first. Meaning before real execution line 6 is interpreted and variable is set to global variable(sort of being pushed all the way up the code).

The solution would be to store the reference to c object then pass it.

[code language=”javascript”] var c = {
name: "this is c object",
log : function(){
var self = this;
// note some developers use "that" instead of self
self.name = "updated c object";
var setName = function(newname){
self.name = newname;
}
setName("Updating again! The c object");
console.log(self);
}
} [/code]

This way, we will not set the name to be global variable and it will be set to c object. So c.name will return “Updating again! The c object”

String … and Generating Javadocs

In java we can pass some arguments in methods. Sometimes we want to pass the array of objects. So if you see writeText(String … text) is actually nothing but array of Strings. It’s same as writeText(String[] text).

To easily Generate javadocs in Intelij Idea you can use this shorcut.

type /** then press enter. This applies for the methods. Later you can add more details to that javadoc

ex.:

/**

public String addText(String[] text){

return ‘abc’;}

becomes

public String addText(String[] text){

return ‘abc’;}