1. What is outcome of x == y ?
Answer: true
== compares values of x and y . Since x and y both has equal values.
2. What will outcome of x === y ?
Answer: false
=== compares both values and type. x is of type String and y is of type Object, It can be verified executing :
typeof x "string"
typeof y "object"
3. What will be outcome of x == y and x === y ?
Answer: Both prints false.
== prints false because both object are different object. And similarly, === prints false because both are different object.
4. What will be outcome of res and res1 ?
Answer: Both will display "Ranjan"
substr(8,6) - starts from index 8 and prints length of 6 char. (start index is 0)
substr(-17,-11) - starts from end(go back 17 chars and then then reaches 11 from end)
5. What will be outcome of below sample unit ?
Answer:
myNumber.toString(16);
"40"
myNumber.toString(8);
"100"
myNumber.toString(2);
"1000000"
6. Write a random number generator function in javascript, provided random number should be generated between min and max passed as input.
Answer:
min is included and max is excluded
min and max both are included
7. Predict outcome of sample code ?
Answer:-
["Apple", "Banana", "Mango", "Orange"]
[1, 10, 100, 25, 40, 5]
First output looks as expected, but what happened to second array output. By default, the sort() function sorts values as strings. So, "25" is bigger than "100", because "2" is bigger than "1".
8. Sort above array(number array) in ascending order ? Hint: Use compare function in sort function
On passing a compare function to sort method, sort method sort numbers as follows.
[1, 5, 10, 25, 40, 100]
Note:- The purpose of the compare function is to define an alternative sort order(instead of natural sort ordering).The compare function should return a negative, zero, or positive value, depending on the arguments:
9. Sorting Object Arrays based on some attributes - Below arrays contains objects. Sort below objects based on salary.
Answer:- positions.sort(function(a, b){return a.Salary - b.Salary});
Above code lines sort positions array of objects in ascending order, same can be validated with following console output
10. Write a compare function to sort array based on type and verify the output.
Answer:
var x = "ZYTHAM"; var y = new String("ZYTHAM");
x==y //prints true/false
== compares values of x and y . Since x and y both has equal values.
2. What will outcome of x === y ?
var x = "ZYTHAM"; var y = new String("ZYTHAM"); x===y
=== compares both values and type. x is of type String and y is of type Object, It can be verified executing :
typeof x "string"
typeof y "object"
3. What will be outcome of x == y and x === y ?
var x = new String("ZYTHAM"); var y = new String("ZYTHAM"); x==y //prints true/false x===y //prints true/false
== prints false because both object are different object. And similarly, === prints false because both are different object.
4. What will be outcome of res and res1 ?
var str = "Nikhil, Ranjan, Devinline"; var res = str.substr(8, 6); var res1 = str.slice(-17, -11);
substr(8,6) - starts from index 8 and prints length of 6 char. (start index is 0)
substr(-17,-11) - starts from end(go back 17 chars and then then reaches 11 from end)
5. What will be outcome of below sample unit ?
var myNumber = 64; myNumber.toString(16); myNumber.toString(8); myNumber.toString(2);
myNumber.toString(16);
"40"
myNumber.toString(8);
"100"
myNumber.toString(2);
"1000000"
6. Write a random number generator function in javascript, provided random number should be generated between min and max passed as input.
Answer:
min is included and max is excluded
function getRndomInteger(min, max) { return Math.floor(Math.random() * (max - min) ) + min; }
min and max both are included
function getRndomInteger(min, max) { return Math.floor(Math.random() * (max - min + 1) ) + min; }
7. Predict outcome of sample code ?
var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.sort(); var points = [40, 100, 1, 5, 25, 10]; points.sort();
["Apple", "Banana", "Mango", "Orange"]
[1, 10, 100, 25, 40, 5]
First output looks as expected, but what happened to second array output. By default, the sort() function sorts values as strings. So, "25" is bigger than "100", because "2" is bigger than "1".
8. Sort above array(number array) in ascending order ? Hint: Use compare function in sort function
var points = [40, 100, 1, 5, 25, 10]; function comparefun(a, b){ return a - b } points.sort(comparefun);
[1, 5, 10, 25, 40, 100]
Note:- The purpose of the compare function is to define an alternative sort order(instead of natural sort ordering).The compare function should return a negative, zero, or positive value, depending on the arguments:
9. Sorting Object Arrays based on some attributes - Below arrays contains objects. Sort below objects based on salary.
var positions = [ {type:"VP", Salary:80}, {type:"Manager", Salary:40}, {type:"Director", Salary:50}];
Above code lines sort positions array of objects in ascending order, same can be validated with following console output
10. Write a compare function to sort array based on type and verify the output.
Answer:
positions.sort(function(a, b){ var x = a.type.toLowerCase(); var y = b.type.toLowerCase(); if (x < y) {return -1;} if (x > y) {return 1;} return 0; });
Tags:
JavaScript
Liên hệ Aivivu, đặt vé máy bay tham khảo
ReplyDeletevé máy bay đi Mỹ bao nhiêu tiền
lịch bay từ mỹ về việt nam
đặt vé từ nhật về việt nam
lich bay tu duc ve viet nam
các đường bay từ canada về việt nam
vé máy bay vietjet từ hàn quốc về việt nam
khách sạn cách ly ở việt nam
vé máy bay chuyên gia nước ngoài sang Việt Nam
Mua vé tại đại lý vé máy bay Aivivu, tham khảo
ReplyDeletegiá vé máy bay đi Mỹ khứ hồi
đăng ký từ mỹ về việt nam
các chuyến bay từ đức về việt nam hôm nay
vé máy bay về việt nam từ nhật
vé máy bay từ hàn về Việt Nam
thông tin chuyến bay từ canada về việt nam
khách sạn cách ly hà nội
chuyen bay danh cho chuyen gia