WK 15 Exercises

Developer
Size
9,460 Kb
Views
36,432

How do I make an wk 15 exercises?

What is a wk 15 exercises? How do you make a wk 15 exercises? This script and codes were developed by Steven on 31 July 2022, Sunday.

WK 15 Exercises Previews

WK 15 Exercises - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>WK 15 Exercises</title> <link rel="stylesheet" href="css/style.css">
</head>
<body> <div id="clock" class="myClock"> <span class="exercise">------- Q8 -------</span> <br> <span class="time" id="theTime">The Time</span>
</div>
<div id="console"> <div> <script src='http://codepen.io/volv/pen/MyqmEZ?editors=0010'></script> <script src="js/index.js"></script>
</body>
</html>

WK 15 Exercises - Script Codes CSS Codes

.exercise { text-align: center; width: 80%; margin: auto; font-weight: bold; color: rgba(54, 86, 150, 1)
}
#console { font-size: 18px; font-weight: bold; overflow-y: auto; background: rgba(29, 31, 32, 1); width: 80vw; margin: auto; color: rgba(109, 157, 119, 1); padding: 10px; line-height: 22px;
}
hr { border: solid 1px; color: rgba(255, 255, 255, 0.1);
}
body { background: rgba(29, 31, 32, 1);
}
.myClock { border: solid rgba(54, 86, 150, 1) 0.5px; background: rgba(29, 31, 32, 1); padding: 0px 5px; position: absolute; text-align: center; right: 15px; top: 15px;
}
.time { font-size: 18px; font-weight: bold; color: rgba(109, 157, 119, 1);
}

WK 15 Exercises - Script Codes JS Codes

"use strict";
console.clear();
(function () { // To keep sample variables seperate console.log("------- Q1 -------"); //Write a JavaScript program to list the properties of a JavaScript object. Go to the editor var student = { name: "David Rayy", sclass: "VI", rollno: 12 }; //Sample Output : name,sclass,rollno var result = ""; for (var key in student) { result += key + ","; }console.log(result.substring(0, result.length - 1)); // name,sclass,rollno
})();
(function () { // To keep sample variables seperate console.log("------- Q2 -------"); //Write a JavaScript program to delete the rollno property from the following object. Also print the object before or after deleting the property. var student = { name: "David Rayy", sclass: "VI", rollno: 12 }; console.log(student); delete student.rollno; console.log(student);
})();
(function () { // To keep sample variables seperate console.log("------- Q3 -------"); //Write a JavaScript program to get the length of an JavaScript object. var student = { name: "David Rayy", sclass: "VI", rollno: 12 }; var result = 0; for (var key in student) { result += 1; } console.log("Length of student object - " + result);
})();
(function () { // To keep sample variables seperate console.log("------- Q4 -------"); //Write a JavaScript program to display the reading status (i.e. display book name, author name and reading status) of the following books. var library = [{ author: 'Bill Gates', title: 'The Road Ahead', readingStatus: true }, { author: 'Steve Jobs', title: 'Walter Isaacson', readingStatus: true }, { author: 'Suzanne Collins', title: 'Mockingjay: The Final Book of The Hunger Games', readingStatus: false }]; for (var _iterator = library, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) { var _ref; if (_isArray) { if (_i >= _iterator.length) break; _ref = _iterator[_i++]; } else { _i = _iterator.next(); if (_i.done) break; _ref = _i.value; } var each = _ref; console.log((each.readingStatus ? "Have Read" : "Not Read") + " - " + each.title + " by " + each.author); }
})();
(function () { // To keep sample variables seperate console.log("------- Q5 -------"); //Write a JavaScript program to get the volume of a Cylinder with four decimal places using object classes. //Volume of a cylinder : V = πr2h // Totally means PI*r^2*h //where r is the radius and h is the height of the cylinder. function Cylinder(r, h) { this.r = r; this.h = h; } Cylinder.prototype.getVolume = function () { return (Math.PI * this.r * this.r * this.h).toFixed(4); }; var volv1 = new Cylinder(1, 1); console.log(volv1.getVolume()); var volv2 = new Cylinder(3, 4); console.log(volv2.getVolume()); var volv3 = new Cylinder(10, 20); console.log(volv3.getVolume());
})();
(function () { // To keep sample variables seperate console.log("------- Q6 -------"); // Write a Bubble Sort algorithm in JavaScript. Go to the editor // Note : Bubble sort is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, // Sample Data : [6,4,0, 3,-2,1] // Expected Output : [-2, 0, 1, 3, 4, 6] function bubbleSort(arr) { var result = Array.from(arr); // Work on copy var length = result.length; var cur = 0, next = 0; // Uses fancy destructuring swap - THIS BREAKS in Edge without Babel. // and presumably in old versions of everything. while (length > 0) { // While still array to be checked for (var i = 0; i < result.length - 1; i++) { cur = result[i];next = result[i + 1]; if (cur > next) { // Pushes biggest to the right ;var _ref2 = [next, cur]; cur = _ref2[0]; next = _ref2[1]; } // Swap } length -= 1; // Don't need to check rightmost again } return result; } console.log([6, 4, 0, 3, -2, 1]); console.log("becomes"); console.log(bubbleSort([6, 4, 0, 3, -2, 1]));
})();
(function () { // To keep sample variables seperate console.log("------- Q7 -------"); // Write a JavaScript program which returns a subset of a string. Go to the editor // Sample Data : dog // Expected Output : ["d", "do", "dog", "o", "og", "g"] function getSubsets(str) { var internalString = str.split(""); // Work on copy var result = []; for (var i = 0; i < internalString.length; i++) { for (var j = i; j < internalString.length; j++) { result.push(internalString.slice(i, j + 1).join("")); } } return result; } console.log(getSubsets("dog"));
})();
(function () { // To keep sample variables seperate // Write a JavaScript program to create a Clock. Go to the editor // Note : The output will come every second. var theTime = new Date(); var hours = "", minutes = "", seconds = ""; var theClock = document.getElementById("theTime"); function clock() { theTime = new Date(); var pad = function pad(x) { return Number(x) < 10 ? "0" + x : "" + x; }; hours = pad(theTime.getHours()); minutes = pad(theTime.getMinutes()); seconds = pad(theTime.getSeconds()); theClock.innerHTML = hours + ":" + minutes + ":" + seconds; } clock(); setInterval(clock, 1000); // Remove first HR make room for clock.. Don't ask document.querySelector("hr").parentElement.removeChild(document.querySelector("hr")); // Scroll with us window.addEventListener("scroll", function (e) { theClock.parentElement.style.top = document.body.scrollTop + 15 + "px"; });
})();
(function () { // To keep sample variables seperate console.log("------- Q9 -------"); function Circle(radius) { this.radius = radius; } Circle.prototype.area = function () { return Math.PI * this.radius * this.radius; }; Circle.prototype.circumference = function () { return Math.PI * this.radius * 2; }; var volvCirc1 = new Circle(10); console.log(volvCirc1); console.log("Area = " + volvCirc1.area()); console.log("Circumference = " + volvCirc1.circumference());
})();
(function () { // To keep sample variables seperate console.log("------- Q10 -------"); // Write a JavaScript program to sort an array of JavaScript objects. // Quick skim of the solution shows they mean sort by any given field. (asc or desc) // Not enough info initially given. // Check the real console for better formatting :) // Sample data var library = [{ title: 'The Road Ahead', author: 'Bill Gates', libraryID: 1254 }, { title: 'Walter Isaacson', author: 'Steve Jobs', libraryID: 4264 }, { title: 'Mockingjay: The Final Book of The Hunger Games', author: 'Suzanne Collins', libraryID: 3245 }]; function sortBy() { var field = arguments.length <= 0 || arguments[0] === undefined ? "author" : arguments[0]; var asc = arguments.length <= 1 || arguments[1] === undefined ? true : arguments[1]; return function (a, b) { a = a[field];b = b[field]; if (a === b) { return 0; } // Equal else { return asc ? a > b ? 1 : -1 : // Ascending b < a ? -1 : 1; // Descending } }; } console.log(library); console.log("LibrayID, Ascending"); console.log(library.sort(sortBy.apply(library, ["libraryID", true]))); // Asc console.log("Author, Ascending"); console.log(library.sort(sortBy.apply(library, ["author", true]))); // Asc console.log("Title, Descending"); console.log(library.sort(sortBy.apply(library, ["title", false]))); // Descending
})();
(function () { // To keep sample variables seperate console.log("------- Q11 -------"); // Write a JavaScript function to print all the methods in an JavaScript object. Go to the editor // Test Data : // console.log(all_properties(Array)); // ["length", "name", "arguments", "caller", "prototype", "isArray", "observe", "unobserve"] function all_properties(obj) { return Object.getOwnPropertyNames(obj); } console.log(all_properties(Array.__proto__));
})();
(function () { // To keep sample variables seperate console.log("------- Q12 -------"); // Write a JavaScript function to parse an URL. // Let browser do the work var url = document.createElement('a'); url.href = "https://www.volv.org:80/test?q1=Pwns&q2=l33t#Mog"; var props = ["protocol", "hostname", "port", "pathname", "search", "hash"]; console.log(url.href); // List each of the above for (var _iterator2 = props, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) { var _ref3; if (_isArray2) { if (_i2 >= _iterator2.length) break; _ref3 = _iterator2[_i2++]; } else { _i2 = _iterator2.next(); if (_i2.done) break; _ref3 = _i2.value; } var each = _ref3; console.log(each + " : " + url[each]); } var query = {}; // Further parse search string into query object url.search.split("?")[1].split("&").forEach(function (each) { query[each.split("=")[0]] = each.split("=")[1]; }); for (var key in query) { console.log(key + " : " + query[key]); }
})();
(function () { // To keep sample variables seperate console.log("------- Q13 -------"); // Write a JavaScript function to retrieve all the names of object's own and inherited properties. function getAllProps(obj) { var hash = new Map(); do { Object.getOwnPropertyNames(obj).forEach(function (each) { hash.set(each); // Map abuse? }); } while (obj = Object.getPrototypeOf(obj)); // Note - assignment - Will fail when no more chain return hash.keys(); // Only care about keys } for (var _iterator3 = getAllProps([42]), _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : _iterator3[Symbol.iterator]();;) { var _ref4; if (_isArray3) { if (_i3 >= _iterator3.length) break; _ref4 = _iterator3[_i3++]; } else { _i3 = _iterator3.next(); if (_i3.done) break; _ref4 = _i3.value; } var each = _ref4; // Properties of an array as you would use. console.log(each); }
})();
(function () { // To keep sample variables seperate console.log("------- Q14 -------"); // Write a JavaScript function to retrieve all the values of an object's properties. var student = { name: "David Rayy", sclass: "VI", rollno: 12 }; function getValues(obj) { var result = []; // Own properties from Object.keys() instead of potential inherited properties // when using for..in for (var _iterator4 = Object.keys(obj), _isArray4 = Array.isArray(_iterator4), _i4 = 0, _iterator4 = _isArray4 ? _iterator4 : _iterator4[Symbol.iterator]();;) { var _ref5; if (_isArray4) { if (_i4 >= _iterator4.length) break; _ref5 = _iterator4[_i4++]; } else { _i4 = _iterator4.next(); if (_i4.done) break; _ref5 = _i4.value; } var each = _ref5; result.push(obj[each]); } return result; } console.log(getValues(student));
})();
(function () { // To keep sample variables seperate console.log("------- Q15 -------"); // Write a JavaScript function to convert an object into a list of `[key, value]` pairs. var student = { name: "David Rayy", sclass: "VI", rollno: 12 }; function keyVal(obj) { var result = []; for (var _iterator5 = Object.keys(obj), _isArray5 = Array.isArray(_iterator5), _i5 = 0, _iterator5 = _isArray5 ? _iterator5 : _iterator5[Symbol.iterator]();;) { var _ref6; if (_isArray5) { if (_i5 >= _iterator5.length) break; _ref6 = _iterator5[_i5++]; } else { _i5 = _iterator5.next(); if (_i5.done) break; _ref6 = _i5.value; } var each = _ref6; result.push([each, obj[each]]); } return result; } console.log(keyVal(student));
})();
(function () { // To keep sample variables seperate console.log("------- Q16 -------"); // Write a JavaScript function to get a copy of the object where the keys have become the values and the values the keys. var student = { name: "David Rayy", sclass: "VI", rollno: 12 }; function turnAround(obj) { // Every now and then... var result = {}; for (var _iterator6 = Object.keys(obj), _isArray6 = Array.isArray(_iterator6), _i6 = 0, _iterator6 = _isArray6 ? _iterator6 : _iterator6[Symbol.iterator]();;) { var _ref7; if (_isArray6) { if (_i6 >= _iterator6.length) break; _ref7 = _iterator6[_i6++]; } else { _i6 = _iterator6.next(); if (_i6.done) break; _ref7 = _i6.value; } var each = _ref7; result[obj[each]] = each; } return result; } console.log(turnAround(student));
})();
(function () { // To keep sample variables seperate console.log("------- Q17 -------"); // Write a JavaScript function to check if an object contains given property. var student = { name: "David Rayy", sclass: "VI", rollno: 12 }; function propExists(obj, prop) { return obj.hasOwnProperty(prop); } console.log(student); console.log("Has property name? - " + propExists(student, "name")); console.log("Has property volv? - " + propExists(student, "volv"));
})();
(function () { // To keep sample variables seperate console.log("------- Q18 -------"); // Write a JavaScript function to check whether a given value is a DOM element. // Apparently one way to do this is to check if the nodeType property exists on an object. // Easily spoofed mind you. function isDOM(obj) { return obj && 'nodeType' in obj; } console.log("document.body on DOM?, " + isDOM(document.body)); console.log("window on DOM?, " + isDOM(window)); console.log("function isDOM on DOM?, " + isDOM(isDOM));
})();
WK 15 Exercises - Script Codes
WK 15 Exercises - Script Codes
Home Page Home
Developer Steven
Username volv
Uploaded July 31, 2022
Rating 3
Size 9,460 Kb
Views 36,432
Do you need developer help for WK 15 Exercises?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Steven (volv) Script Codes
Create amazing marketing copy with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!