0% found this document useful (0 votes)
27 views3 pages

JSEXAM

Uploaded by

naveedprotek
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views3 pages

JSEXAM

Uploaded by

naveedprotek
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Javascript Exam

No Devices Allowed

Name: ________________________________________

Score:________

1. const arr = [10, 12, 15, 21];


for (var i = 0; i < arr.length; i++)
{ setTimeout(function() {
console.log('Index: ' + i + ', element: ' + arr[i]); }, 3000);
}

What will be printed to the console? Explain Why?

2. Given a string, find out if it satisfies the IPv4 address naming rules.
Example: For inputString = "172.16.254.1", the output should be
isIPv4Address(inputString) = true
For inputString = "172.316.254.1", the output should be
isIPv4Address(inputString) = false

3. What is a closure function? Write an example of a closure function!

Answer: Closure function is a function, which has another function inside of


it, the inner function will be able to use the values stored in the outer
functions variables.

function myfunction(){
var a = 10;
function innerfunction(){
var c;
var b = 10;
c=a+b;
console.log(c);
}
return innerfunction;
}
4. Write a function that takes a string, and returns the longest word in that
string?
For example : largest(“Find the largest word here”) will return “largest”.
Answer: longestword(“Find the largest word here”);
function longestword(mystring){
var stringtoarray = mystring.split(“ “);
var longest = stringtoarray.reduce(a,b)
if(b.length > a.length){
return b;}
else{
return a;}
return longest;

5. Write a function which finds the second largest number in an array and
returns it? If there are less then two numbers in array it just returns -1.

6. What will be output to following code:


var x = “John”.split(‘’);
var y = “Jones”.split(‘’); -
var z = x
z.push(y);
console.log(x.length) - 4
console.log(y.length) - 5
console.log(x.slice(-1)) - n

7. DOM project provided in class.

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
</head>
<body>
<input type="text" id="myvalue"><br>
<input type="text" id="mycolor"><br>
<button onclick="addvalues()">Submit</button>
<div id="container"></div>
<script>
window.onload = function(){
var items = localStorage.getItem("items")
if (items) {
var arr = JSON.parse(items);
for (var i = 0; i < arr.length; i++) {
var p = document.createElement("p");
var tn = document.createTextNode(arr[i]);
p.appendChild(tn);
container.appendChild(p);
}
}
};
var nvalue = document.getElementById("myvalue");
var ncolor = document.getElementById("mycolor");
var container = document.getElementById("container");
var arr = JSON.parse(localStorage.getItem("items")) || [];
function addvalues(){
var fvalue = nvalue.value;
var fcolor = ncolor.value;
var objinsert = {myvalue: fvalue, mycolor: fcolor};
arr.push([objinsert]);
var p = document.createElement("p");
var tn = document.createTextNode(name);
p.appendChild(tn);
container.appendChild(p);
nvalue.value = "";
ncolor.value = "";
nvalue.focus();
if (fvalue != "") {
localStorage.setItem("items", JSON.stringify(arr));
}
}
</script>

</body>
</html>

You might also like