159k views
5 votes
Answer the following questions:

1. What is the value of foo?
var foo = 10 + '20';
2. What will be the output of the code below?
console.log(0.1 + 0.2 == 0.3);
3. How would you make this work?
add(2, 5); // 7 add(2)(5); // 7
4. What value is returned from the following statement?
"i'm a lasagna hog".split("").reverse().join("");
5. What is the value of window.foo?
( window.foo || ( window.foo = "bar" ) );
6. What is the outcome of the two alerts below?
var foo = "Hello"; (function() { var bar = " World"; alert(foo + bar); })(); alert(foo + bar);
7. What is the value of foo.length?
var foo = []; foo.push(1); foo.push(2);
8. What is the value of foo.x?
var foo = {n: 1}; var bar = foo; foo.x = foo = {n: 2};
9. What does the following code print?
console.log('one'); setTimeout(function() { console.log('two'); }, 0); Promise.resolve().then(function() { console.log('three'); }) console.log('four');
10. What is the difference between these four promises?
doSomething().then(function () { return doSomethingElse(); }); doSomething().then(function () { doSomethingElse(); }); doSomething().then(doSomethingElse()); doSomething().then(doSomethingElse);

1 Answer

2 votes

Answer:

The answer to the following questions can be given as:

Question: 1

value of foo is = 1020

Question: 2

Output of the code is = false

Question: 3

function add(a, b)

{

return a && b ? a + b : function (c) { return a + c; };

}

var a=add(2,5);

var x=add(2)(5);

print(a);

print(x);

output:

7

7

Question: 4

"goh angasal a m'i"

Question: 5

In this we check that if in the window a foo function exists. otherwise we set windows.foo to bar.

Question: 6

first alert shows the value "Hello World";

second alert shows value ReferenceError, In this bar not defined.

Question: 7

The value of foo.length is 2

Question: 8

The value of foo.x is undefined

Question: 9

Function print = "one", "three", "two" .

Question :10

It is difficult to define because doSomethingElse() function is not defined.

Explanation:

In question 1 the value of the foo variable is 1020 because we perform the string concat operation.

In question 2 Output of the code is false because float value keeps in binary and when we convert 0.1 or 0.2 to binary so it is not the same.

In question 3 the output of both functions is 7.

In question 4 the output is goh angasal a m'i because In this code we use the reverse() function, split() function and join() function. Reverse function reverses the value. split function splits the value and join function join all the value of the string in a line.

In question 5 we check the value.

In question 6, the first output is Hello World and second is ReferenceError.

In question 7, the value of foo.length is 2 because this foo array we insert only 2 values.

In question 8, the value of foo.x is undefined because in the variable we reset the value of foo all together, so foo.x value is undefined.

In the question 9 function print "one",

"three",

"two" because In the function first, we print the message that is "one" then we define a function in this function we print the value

"two" but before creating object we call the "three" so the print value is "one",

"three",

"two".

In question 10 function, the output of this function is undefined.

User Byron Ruth
by
6.5k points