Question 1-5

by cassfinn

HTML

<h3>Question 1</h3>

An Uncaught Reference Error is generated because b is not declared outside of the function.
The solution is to declare var b outside of the function, before console.log(b) is called.
<br>

<h3>Question 2</h3>

10 will be printed from the log statement because it is returned from the inner function.  var a is undefined when console.log(a) is called, so its value of 5 will not be printed.  To correct this, var a should be initialized to 5 before console.log(a) is called.
<br>

<h3>Question 3</h3>
Different values are printed because function() is called first from obj.attr, which refers to the property name in obj.attr, "Third Entity".  The value of the variable obj.attr.getName is the function and the function is assigned to the variable test. test() runs the function outside of the object where this.name refers to var name, 'First Entity'.  
<br>

<h3>Question 4</h3>

var nums = [21, 13, 2, 19, 40, 17, 49, 34, 91, 8];<br>
const len = nums.length;<br>
const midPoint = nums.length / 2;<br>
<br>
for (i = 0; i < len; i++) { <br>
<div class='a'>
	if (i < midPoint) {
  <div class='b'>
		console.log(nums[i] + " is at position " + i + " (Available in First half of array");
    </div></div>
    <div class='a'>
	} else {
  <div class='b'>
		console.log(nums[i] + " is at position " + i + " (Available in Second half of array");	 
    </div></div>
        <div class='a'>
        }  </div>
};  

<br>
<h3>Question 5</h3>
Merge and Sort two arrays:<p>
var arr1 = [10, 112, 82, 13, 67, 12];<br>
var arr2 = [91, 12, 65, 21, 10, 91, 16, 75];<br>
var arr3 = [...arr1, ...arr2];<br>
arr3.sort(function(a, b) { return b < a }); <br>

CSS

div.a {
    text-indent: 1em;
}

div.b {
    text-indent: 2em;
}

div.c {
    text-indent: 3em;
}

JavaScript

// Question 1
// before
(function() {
	'use strict'
	var a = b = 10;
})();
console.log(b);

// after
// declare b outside of the function
var b;
(function() {
	'use strict'
	var a = b = 10;
})();
console.log(b);


// Question 2
// before
function main() {
	console.log(a);
	console.log(inner());
	var a = 5;
	function inner() {
		return 10;
	}
}
main();

// after
function main() {
	var a = 5;
	console.log(a);
	console.log(inner());
	function inner() {
		return 10;
	}
}
main();


// Question 3
// Explain why different values are printed
var name = 'First Entity';
var obj = {
	name: 'Second Entity',
	attr: {
		name: 'Third Entity',
		getName: function() {
			return this.name;
		}
	}
};
console.log(obj.attr.getName());

// function is assigned to var test
var test = obj.attr.getName;

// function getName() is called outside of the object 
console.log(test());


// Question 4
var nums = [21, 13, 2, 19, 40, 17, 49, 34, 91, 8];
const len = nums.length;
const mid = nums.length / 2;

for (i = 0; i < len; i++) {
	if (i < mid) {
		console.log(nums[i] + " is at position " + i + " (Available in First half of array");
	} else {
		console.log(nums[i] + " is at position " + i + " (Available in Second half of array");	
	}
};


// Question 5
var arr1 = [10, 112, 82, 13, 67, 12];
var arr2 = [91, 12, 65, 21, 10, 91, 16, 75];
var arr3 = [...arr1, ...arr2];
// ensure the array is sorted numerically
arr3.sort(function(a, b) { return b < a }); 


// Question 6