Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Javascript Help: 51. How can you match any metacharacters as literal values in a

ID: 3737439 • Letter: J

Question

Javascript Help:

51. How can you match any metacharacters as literal values in a regular expression?

52. Provide two different regular expressions that return true only when tested against a string consisting of exactly 5 characters.?

54. How can you add options to a selection list with JavaScript? 58, Explain the relationship between the Window object and the Document object.

59. What is the difference in syntax between referencing a CSS property of an element and referencing an attribute of an element? Give an example of each.

60. Name two ways to add a node to the DOM, and explain when you'd use each.

62. What is the primary difference between the search() method and the indexOf() method?

61. The second p element in a web page is created with the following tag pair: <p id="info" class="mypar"></p>. Provide three different examples of code to reference this element using three different methods.

Explanation / Answer

51.How can you match any metacharacters as literal values in a regular expression?
A metacharacter is a character that has a special meaning (instead of a literal meaning) to a computer program.
A regular expression is a text pattern consisting of a combination of alphanumeric characters and special characters known as metacharacters.
Actually in the world of regular expressions any character which is not a metacharacter will match itself (often called literal characters),
however a lot of the time you are mostly concerned with the alphanumeric characters.
eg:-A very special character is the backslash , as this turns any metacharacters into literal characters.

| ( ) [ { ^ $ * + ? . < >


54.How can you add options to a selection list with JavaScript?
There are a variety of approaches for adding option elements in a select box using JavaScript.
An old method is option constructor and array syntax.
Both select and options objects provide add methods.Another method is Add Option Using DOM Methods
var sel = document.getElementById('selDemo');
=> document.createElement method is used to create an option element.
// create new option element
var opt = document.createElement('option');

=> document.createTextNode method is used to add text to the option.
// create text node to add to option element (opt)
opt.appendChild( document.createTextNode('New Option Text') );

=> use either the setAttribute method or dot syntax to add a value attribute to the option element.
// set value property of opt
opt.value = 'option value';

=> To add the new option element to the end of the list of options, use the appendChild method.
// add opt to end of select box (sel)
sel.appendChild(opt);

58.Explain the relationship between the Window object and the Document object?
WINDOW object and DOCUMENT object are not the same.

Window object
**************
=> window is the first thing that gets loaded into the browser.
=> properties like length, innerWidth, innerHeight, name, if it has been closed, its parents, and more.
=> window object is the browser instance and contains the document and screen objects

Document object
****************
=> The document object is your html, aspx, php, or other document that will be loaded into the browser.
=> properties available to it like title, URL, cookie, etc.
=> The document object is the base object of the Document Object Model (DOM)

59.What is the difference in syntax between referencing a CSS property of an element and referencing an attribute of an element?
Give an example of each.
The style property returns a CSSStyleDeclaration object, which represents an element's style attribute.
The style property is used to get or set a specific style of an element using different CSS properties.
eg:element.style.backgroundColor = "red";

The attr() method sets or returns attributes and values of the selected elements.
When this method is used to return the attribute value, it returns the value of the FIRST matched element.
When this method is used to set attribute values, it sets one or more attribute/value pairs for the set of matched elements.

60. Name two ways to add a node to the DOM, and explain when you'd use each.
To add a new element to the HTML DOM, you must create the element (element node) first, and then append it to an existing element.
To add text to the <p> element, you must create a text node first. This code creates a text node:
var node = document.createTextNode("This is a new paragraph.");
Then you must append the text node to the <p> element:

para.appendChild(node);
The appendChild() method adds a child node to an existing node.
62. What is the primary difference between the search() method and the indexOf() method?
If you require a regular expression, use search(). Otherwise, indexOf() is going to be faster.
indexOf is for plain substrings, search can do regular expressions.
The search function takes a regular expression, which allows you to match against more sophisticated patters,
case-insensitive strings, etc., while indexOf simply matches a literal string.
However, indexOf also allows you to specify a beginning index.

52.Provide two different regular expressions that return true only when tested against a string consisting of exactly 5 characters.?
A regular expression is a type of object.
It can either be constructed with the RegExp constructor or written as a literal value by enclosing a pattern in forward slash (/) characters.
Regular expression objects have a number of methods. The simplest one is test. If you pass it a string, it will return a Boolean telling you whether the string contains a match of the pattern in the expression.

console.log(/abc/.test("abcde"));
// ? true
console.log(/abc/.test("abxde"));
// ? false
let re1 = new RegExp("abc");
let re2 = /abc/;

61. The second p element in a web page is created with the following tag pair: <p id="info" class="mypar"></p>.
Provide three different examples of code to reference this element using three different methods.
document.getElementById(info)
document.getElementsByTagName(p)
document.getElementsByClassName(mypar)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote