JavaScript
11. What is replace() Give examples
- a method - replaces a part of a string with another string - Only the first occurrence is replaced - examples let str = 'JavaScript'; let newstr = str.replace('Java', 'ECMA'); 'Java' is replaced with 'ECMA'. Thus newstr has the value 'ECMAScript'. The original string remains unchanged, except for the first instance
1. What is an alert box?
- a popup box - used when you want to ensure that information gets through to the user - the user must click OK to proceed or interact further with the page - takes a single parameter, which is the text displayed in the popup box
26. What is an associative array? Does JavaScript support associative arrays?
- an array with named indexes (text instead of numbers) - no
27. What happens if you try to make an associative array in javascript?
- it is treated as an object and none of the methods and properties that work on arrays will work for it
6. What is a confirm box?
- often used to have th user verify or accept something - when a confirm box pops up, the user must click either OK or cancel to proceed - if the user clicks OK, the box returns true - if the user clicks cancel, the box returns false - do not overuse this method, because it also prevents the user from accessing other parts of the page until the box is closed
21. What is Math.random()
- a function of the Math object - generates a psuedo random number between 0 (inclusive) and 1 (exclusive) - each call generates a new random number
19. What is Math.floor()
- a function of the Math object - rounds downwards to the nearest integer
18. What is Math.round()
- a function of the Math object - rounds to the nearest integer
20. What is Math.ceil()
- a function of the Math object - rounds upwards to the nearest integer
13. What is Math.pow(input1, input2)
- a function of the math object - calculates input1 to the power of input2
15. What is Math.max(input1, input2, .....)
- a function of the math object - calculates the maximum
14. What is Math.min(input1, input2, ....)
- a function of the math object - calculates the minimum
12. What is Math.sqrt()
- a function of the math object - calculates the square root
17. What is Math.E
- a mathematical constant offered by the Math object - gives Euler's number (2.71)
16. What is Math.PI
- a mathematical constant offered by the Math object - gives pi (roughly 3.14)
22. What is concat()
- a method - allows you to join arrays - creates an entirely new array - does not change the original arrays that it is pulling from
9. What is substr()
- a method - extracts a substring from a string - the first parameter specifies the position at which to start - The second parameter specifies the number of characters to extract - If the second parameter is not set, all the characters from start position to the end of the string are extracted - https://www.jshero.net/en/koans/stringsubstr.html
2. How do you display a line break within a popup box?
\n ex: alert("hello\nHow are you?");
5. example syntax of alert
alert("what you want to say");
28. What should you use instead of an associative array?
an object
25. How do you access an element from inside an array?
console.log(arrayName[#]);
23. Give an example of concat()
var c1 = ['HTML', 'CSS']; var c2 = ['JS', 'C++']; var courses = c1.concat(c2); console.log(courses); output: HTML,CSS,JS,C++
24. How do you create an array?
var name = new Array('thing one', 'thing two', 'thing three); OR var name = new Array(#ofelements); name[0] = 'html'; name[1] = 'js'; or var name = ['html', 'js'];
7. example syntax for confirm box
var result = confirm("Do you really want to leave this page?"); if (result == true) { alert("Thanks for visiting"); } else { alert("Thanks for staying with us"); }
4. example syntax of prompt
var user = prompt("your prompt here"); alert(user);
3. What is a prompt box?
- often used to have the user input a value before entering a page - the user will have to click either OK or cancel to proceed after entering the input value - if the user clicks OK, the box returns the input value - if the user clicks cancel, the box returns null - the user must click OK or cancel to proceed after entering an input value - do not overuse this because it prevents the user from accessing other parts of the page until the box is closed - takes two parameters --- the first is the label, which you want to display in the text box --- the second is a default string to display in the text box (optional)
10. What will be the values of the following variables A) let see = 'see and stop'.substr(0, 3); B) let and = 'see and stop'.substr(4, 3); C) let stop = 'see and stop'.substr(8);
A) 'see' B) 'and' C) 'stop'