Javascript 1 Flashcards
Which way is the slash to escape characters in a string?
\ (backslash)
How to escape these characters... newline tab quote backslash
\n newline
\t tab
" quote
\ backslash
How to popup a prompt to ask for input and save to a variable?
Use the window object's prompt method: let name = window.prompt( "Please enter your name" );
What’s the difference between a function and a method?
A method is a function that belongs to an object (must be preceded by object and dot).
What object is writeln() a method of?
What object is prompt() a method of?
document. writeln(“hello”);
window. prompt(“Enter a number”);
What is parseInt()’s optional second argument?
The radix is the 2nd argument. By default it’s 10 for base 10, but you can provide 8 for octal or 16 for hexadecimal.
switch ( n )
{
case 1:
document.writeln( “The number is 1” );
case 2:
document.writeln( “The number is 2” );
break;
default:
document.writeln( “The number is not 1 or 2” );
break;
Missing break on case 1 will cause fallthrough and case 2 executing.
Sum the odd integers between 1 and 99.
sum = 0;
for ( count = 1; count <= 99; count += 2 )
sum += count;
Print the integers from 1 to 20 by using a while loop and the counter variable x. Assume
that the variable x has been declared, but not initialized. Print only five integers per line.
[Hint: Use the calculation x % 5. When the value of this expression is 0, start a new
paragraph in the HTML5 document.
let x = 1; document.writeln( "<p>" );
while ( x <= 20 ) { document.write( x + " " ); if ( x % 5 == 0 ) document.write( "</p><p>" ); \++x; }</p>
T or F: The default case is required in the switch selection statement.
False. If no default action is needed, then there’s no need for a default case.
T or F: The break statement is required in the last case of a switch selection statement.
False.
Find the maximum of three floating point values
Math.max(x, Math.max(y, z));
Display each integer with a 10px margin between them (margin-right)
——————–
Consider an inline list.
p, ol { margin: 0; }
li { display: inline; margin-right: 10px; }
var value; document.writeln( "<p>Random Numbers</p><ol>" );
for ( var i = 1; i <= 30; ++i ) { value = Math.floor( 1 + Math.random() * 6 ); document.writeln( "<li>" + value + "</li>" ); } // end for document.writeln( "</ol>" );
Roll-dice (display random images)
Random Dice Images
li { display: inline; margin-right: 10px; } ul { margin: 0; }
// variables used to interact with the img elements var die1Image; var die2Image; var die3Image; var die4Image;
// register button listener and get the img elements function start() { var button = document.getElementById( "rollButton" ); button.addEventListener( "click", rollDice, false ); die1Image = document.getElementById( "die1" ); die2Image = document.getElementById( "die2" ); die3Image = document.getElementById( "die3" ); die4Image = document.getElementById( "die4" ); } // end function start
// roll the dice function rollDice() { setImage( die1Image ); setImage( die2Image ); setImage( die3Image ); setImage( die4Image ); } // end function rollDice
// set src and alt attributes for a die function setImage( dieImg ) { var dieValue = Math.floor( 1 + Math.random() * 6 ); dieImg.setAttribute( "src", "die" + dieValue + ".png" ); dieImg.setAttribute( "alt", "die image with " + dieValue + " spot(s)" ); } // end function setImage window.addEventListener( "load", start, false );
<ol> <li><img></li> <li><img></li> <li><img></li> <li><img></li> </ol>
Submit
Forget
Both buttons submit the form. In order to determine which one was clicked in Javascript or PHP you’d need to give id or other solution.