JAVASCRIPT QUESTIONS AND ANSWERS, Exercises of Javascript programming

6.0 JAVASCRIPT QUESTIONS AND ANSWERS 2025

Typology: Exercises

2024/2025

Available from 03/19/2025

MENJA.ONLINE
MENJA.ONLINE 🇰🇪

7 documents

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
JAVASCRIPT QUESTIONS AND ANSWERS
1. How do you test that a variable contains a valid number?
isNaN(varHere); //Returns true or false.
2. How do you display a confirmation?
confirm("Message Text Here");
3. What values can confirm() return?
True or false
4. How do you assign the return value of confirm() to a variable?
var answerVar = confirm("Message Text Here");
5. How do you access a page element by id?
document.getElementById(id);
6. How do you assign actions to the window onLoad event?
window.onload = function() {//actions here}
7. What are the four attributes of the <script> tag?
Type, Src, Charset, and Defer
8. In the <script> tag, how is the Type attribute used?
<script type="text/javascript"> //Used to denote the type of script being
used. Always this for JavaScript
9. In the <script> tag, how is the Src attribute used?
<script src="fileName.js"> //Used to denote external file for script use
10. In the <script> tag, how is the Defer attribute used?
<script defer="defer"> //Used to ensure the code doesn't run until the rest of
the page has been loaded
11. What is the command to display a prompt?
prompt("Text Here");
12. How do you assign the value from a prompt to a string?
var stringVar = prompt("Text Here");
13. How do you assign the value from a prompt to a non-string variable?
Using a parse. Example : var intVar = parseInt(prompt("Text Here"));
14. How do you assign a default value to a prompt?
Using the second parameter. Example: prompt("Enter Age:", "18");
15. How do you display an alert?
alert("Alert Text Here");
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download JAVASCRIPT QUESTIONS AND ANSWERS and more Exercises Javascript programming in PDF only on Docsity!

JAVASCRIPT QUESTIONS AND ANSWERS

  1. How do you test that a variable contains a valid number? isNaN(varHere); //Returns true or false.
  2. How do you display a confirmation? confirm("Message Text Here");
  3. What values can confirm() return? True or false
  4. How do you assign the return value of confirm() to a variable? var answerVar = confirm("Message Text Here");
  5. How do you access a page element by id? document.getElementById(id);
  6. How do you assign actions to the window onLoad event? window.onload = function() {//actions here}
  7. What are the four attributes of the 16.What is the tag used for? The text between the opening and closing tag is displayed if JavaScript is disabled or otherwise not available 17.How do you write a line to the current element of the DOM? document.writeln("Text Here"); //Advances to new line after text 18.What is the DOM? Document Object Model 19.How do you declare a variable in JavaScript? var variableName; 20.How do you create a Date object in JavaScript? var dateVar = new Date(); 21.How do you create an Array object in JavaScript? var arrayVar = new Array(); 22.How do you view the current web address using JavaScript? window.location(); 23.How do you make the browser load a new page using JavaScript? window.location = "New Web Address Here"; 24.How do you write text to the current element of the DOM? document.write("Text Here"); //Remains on current line 25.What method allows you to set the digit precision of a decimal number? toFixed(digitCount); 26.For textboxes, How do you get the current value? document.getElementById("TextBoxId").value; 27.For textboxes, How do you set the control to be disabled? document.getElementById("TextBoxId").disabled = true; //Could also be false to enable it 28.For textboxes, how do you set focus on the control? document.getElementById("TextBoxId").focus; 29.How do you code an If statement in JavaScript? if (condition) {} 30.How do you code an If Else statement in JavaScript? if (condition){ } else {}

45.How do you code a button.onclick event handler? document.getElementById("ButtonId").onclick = functionName; 46.For radio buttons, how do you get the text value of the control? document.getElementById("radioButtonId").value; 47.For radio buttons, how do you get the current checked status of the control? document.getElementById("radioButtonId").checked; 48.For checkboxes, how do you get the text value of the control? document.getElementById("checkboxId").value; 49.For checkboxes, how do you get the current checked status of the control? document.getElementById("checkboxId").checked; 50.For lists, how do you get the value or the currently selected item? document.getElementById("listId").value; 51.For a text area, how do you get the current value of the control? document.getElementById("textAreaId").value; 52.For a text area, how do you get the current character count for the control? var areaText = document.getElementById("textAreaId").value; var charCount = areaText.length; 53.For radio buttons, how do you set the current checked status of the control? document.getElementById("radioButtonId").checked = true; //Could also be false 54.For checkboxes, how do you set the current checked status of the control? document.getElementById("CheckboxId").checked = true; //Could also be false 55.For a text area, how do you set the current value of the control? document.getElementById("textAreaId").value = "Text Value"; 56.What are the available properties for the Number object? Number.MAX_VALUE Number.MIN_VALUE Number.POSITIVE_INFINITY Number.NEGATIVE_INFINITY Number.NaN

57.What are the 3 shortcuts for Number object properties? Infinity //Represents Number.POSITIVE_INFINITY

  • Infinity //Represents Number.NEGATIVE_INFINITY NaN //Represents Number.NaN 58.What Number object method is used to round numbers to the specified number of decimal places? toFixed(digits); 59.What Number object method is used to return a string with a given number base? toString(base); //Bases can range from 2 to 36 60.What Number object method is used to return a number in exponential format with the specified number of decimal places? toExponential(digits); 61.What Number object method is used to return a numerical string with the specified number of significant digits? toPrecision(precision); 62.What is the syntax of a conditional operator? (Condition_Expression)? Value_If_True : Value_If_False; 63.What Math object method is used to return the absolute value of a given number? Math.abs(number); 64.What are the two methods common to most controls? focus //Brings focus to the control blur //Removes focus from the control 65.What are the events common to most controls? onfocus //Fired when control receives focus onblur //Fired when control loses focus onclick //Fired when the control is clicked ondblclick //Fired when the control is double clicked onchange //Fired when the control's value is changed onselect //Fired when text is selected in a textbox or text area 66.How do you concatenate multiple parts into a string? var stringVar = "part 1:" + "part 2"; 67.How do you access the text element in a tag? document.getElementById("spanId").firstChild;

81.What String object method is used to get the character at a specified index position? charAt(position); 82.What String object method is used to concatenate multiple strings? concat(var1, var2, varN); 83.What String object method is used return the position of the first instance of a specified search string, starting from the specified index? indexOf(searchValue, startPosition); //If no startPosition is specified, the search begins from the start of the string 84.What String object method is used to return a new string that contains part of the original string from the specified start position? substring(startIndex); 85.What String object method is used to return a new string that contains part of the original string from the specified start position and up to but not including the specified stop index? substring(startIndex, stopIndex); //stopIndex is optional, if it is not specified, then it will go until the end of the string 86.What String object method returns a new string containing the value of the original string but in all upper case? toUpperCase(); 87.What String object method returns a new string containing the value of the original string but in all lower case? toLowerCase(); 88.What Math object method is used to return a given number raised to a given power? Math.pow(number, power); 89.What Math object method is used to return the square root of a given number? Math.sqrt(number); 90.What Math object method is used to return the highest value from a set of supplied numbers? Math.max(var1, var2, varN); 91.What Math object method is used to return the lowest value for a set of supplied numbers? Math.min(var1, var2, varN);

92.How do you create a new Date object? var dateObject = new Date(year, month, day, hours, minutes, seconds, milliseconds); 93.What is the format to create a new Date object from a string? var dateObject = new Date("11/22/2012 18:25:35"); 94.What Date object method is used to return the number of milliseconds since the start of GMT? getTime(); 95.What are the available get methods for a Date object? getTime(); getFullYear(); //Returns 4 digit year getMonth(); //Returns months, starts with 0 for January getDate(); // Returns day of the month getHours(); //Returns hours in the day getMinutes(); //Returns the minutes in the current hour getSeconds(); //Returns seconds in the current minute getMilliseconds(); //Returns milliseconds in the current second 96.What are the available set methods for a Date object? setFullYear(); //Sets 4 digit year setMonth(); //Sets months, starts with 0 for January setDate(); // Sets day of the month setHours(); //Sets hours in the day setMinutes(); //Sets the minutes in the current hour setSeconds(); //Sets seconds in the current minute setMilliseconds(); //Sets milliseconds in the current second 97.What Date object method is used to return a string containing the date and time? toString(); 98.What Date object method is used to return a string containing the date? toDateString(); 99.What Date object method is used to return a string containing the time? toTimeString();

  1. What makes the identity operators different from the standard equality operators? They do not perform type coercion. //Eg. If data types don't match they fail the comparison