- [] + []; // ""
- {} + {}; // NaN
- [] + {}; // "[object Object]"
- {} + []; // 0
- [] == []; // false
- [] < []; // false
- [] > []; // false
- [] <= []; // true
- [] >= []; //true
- Math.min() < Math.max(); // false
- foo = [0];
- !foo; // false
- foo == foo; // true
- foo == !foo; // true
- [['0']] == false; // true
- [['0']] == true; // false
- typeof NaN; // "number"
- NaN == true; // false
- NaN == false; // false
- NaN != true; // true
- NaN != false; // true
- NaN == NaN; // false
- NaN == !NaN; // false
- !NaN; // true
- NaN != NaN; // true
- 'x' == true; // true
- 'x' == false; // false
- '0' == true; // false
- '0' == false; // true
All about very small logical and programming problems. Complex theories made simple to understand
Showing posts with label JavaScript. Show all posts
Showing posts with label JavaScript. Show all posts
Wednesday, July 30, 2014
Strangest things i found in JavaScript
Friday, July 25, 2014
Couple of interview questions
Write a function that takes an integer (i) and prints out the first 'i' rows of Pascal's Triangle (eg. i =5).
line = 5;
var pascalDepth = parseInt(line, 10); //The input integer is provided on a single line as a text value, so this handles it.
var testTriangle = pascalTriangle(pascalDepth);
var output = [];
maxlength = testTriangle.length;
var html = "<table>"
for(var i = 0; i < maxlength; i++){
html += "<tr>";
html += "<td>";
html += testTriangle[i];
html += "</td>";
html += "</tr>";
}
html+= "</table>";
document.write(html);
function pascalTriangle(totalLines, triangle) {
if (typeof triangle === 'undefined') {
triangle = [];
}
if (triangle.length === totalLines) {
return triangle;
}
if (triangle.length > 0) {
var triangleLine = [];
for (var i = 0; i < triangle.length; i++) {
if (typeof triangle[triangle.length - 1][i - 1] === 'undefined') {
triangleLine.push(1);
} else {
triangleLine.push((triangle[triangle.length - 1][i - 1] + triangle[triangle.length - 1][i]));
}
}
triangleLine.push(1);
triangle.push(triangleLine);
} else {
triangle.push([1]);
}
return pascalTriangle(totalLines, triangle);
}
-----------------------------------------------------------------------------------------
var pascalDepth = parseInt(line, 10); //The input integer is provided on a single line as a text value, so this handles it.
var testTriangle = pascalTriangle(pascalDepth);
var output = [];
maxlength = testTriangle.length;
var html = "<table>"
for(var i = 0; i < maxlength; i++){
html += "<tr>";
html += "<td>";
html += testTriangle[i];
html += "</td>";
html += "</tr>";
}
html+= "</table>";
document.write(html);
function pascalTriangle(totalLines, triangle) {
if (typeof triangle === 'undefined') {
triangle = [];
}
if (triangle.length === totalLines) {
return triangle;
}
if (triangle.length > 0) {
var triangleLine = [];
for (var i = 0; i < triangle.length; i++) {
if (typeof triangle[triangle.length - 1][i - 1] === 'undefined') {
triangleLine.push(1);
} else {
triangleLine.push((triangle[triangle.length - 1][i - 1] + triangle[triangle.length - 1][i]));
}
}
triangleLine.push(1);
triangle.push(triangleLine);
} else {
triangle.push([1]);
}
return pascalTriangle(totalLines, triangle);
}
-----------------------------------------------------------------------------------------
Write a function that prints out a breakdown of an integer into a sum of numbers that have just one non-zero digit.
eg., given 43018 it should print 40000 + 3000 + 10 + 8.
var arr = [];
var i =1;
var a = 40118;
var input = 40118;
while(a > 0)
{
if(a%(Math.pow(10,i)) > 0)
arr.push(a%(Math.pow(10,i)));
a = a-a%(Math.pow(10,i++));
}
arr.sort(function(a,b){ return b-a;});
document.write(input +" = " +arr.join(" + "));
var i =1;
var a = 40118;
var input = 40118;
while(a > 0)
{
if(a%(Math.pow(10,i)) > 0)
arr.push(a%(Math.pow(10,i)));
a = a-a%(Math.pow(10,i++));
}
arr.sort(function(a,b){ return b-a;});
document.write(input +" = " +arr.join(" + "));
Wednesday, May 7, 2014
Swap 2 numbers without using third variable
This is a very very very simple question generally asked as first level of logical questions
Swap 2 numbers without using third variable
function fnSwap(a,b){
console.log("Initial value of a: "+ a);
console.log("Initial value of b: "+ b);
// swaping the values of a and b
b = a+b
a = b-a
b = b-a
console.log("Swaped value of a: "+ a);
console.log("Swaped value of b: "+ b);
}
Input:
fnSwap(10,20);
output:
Note:
It works with console if you are working with firefox install firebug to see output in console
if you install chrome/IE you can use f12 for console
Swap 2 numbers without using third variable
function fnSwap(a,b){
console.log("Initial value of a: "+ a);
console.log("Initial value of b: "+ b);
// swaping the values of a and b
b = a+b
a = b-a
b = b-a
console.log("Swaped value of a: "+ a);
console.log("Swaped value of b: "+ b);
}
Input:
fnSwap(10,20);
output:
Initial value of a: 10
Initial value of b: 20
Swaped value of a: 20
Swaped value of b: 10
Note:
It works with console if you are working with firefox install firebug to see output in console
if you install chrome/IE you can use f12 for console
Thursday, March 22, 2012
Closing the PopUp and redirecting Parent Window
I have tried for few hours to solve this problem Here is the simple example which helps you to solve the Problem
Problem:
Create a PopUp window on submission of a value in PopUp the PopUp Closes and the Parent Windows redirects to Some other Page
Solution:
Here is the simple way to solve it
Create 2 php pages
1. popup.php
Copy this Code
<html>
<head>
</head>
<body>
<a href="#" onclick="window.open('popupcode.php','Login','height=500,width=420,scrollbars=yes');return false;">PopUp</a>
</body>
</html>
Problem:
Create a PopUp window on submission of a value in PopUp the PopUp Closes and the Parent Windows redirects to Some other Page
Solution:
Here is the simple way to solve it
Create 2 php pages
1. popup.php
Copy this Code
<html>
<head>
</head>
<body>
<a href="#" onclick="window.open('popupcode.php','Login','height=500,width=420,scrollbars=yes');return false;">PopUp</a>
</body>
</html>
Subscribe to:
Posts (Atom)