Javascript sintaxis Flashcards

cheat sheet javascript

1
Q

Function: Delay - 1 second timeout

A

setTimeout(function () { }, 1000);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Functions, example: add numbers (1 + 2)

A

function addNumbers(a, b)
{ return a + b; }
x = addNumbers(1, 2);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Edit DOM element by ID

A

document.getElementById(“elementID”).innerHTML

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Output console

A

console.log(a); // write to the browser console

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Output prompt

A

prompt( “Your age?”, “0” );
// input dialog.
// Second argument is the initial value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Output write to the html

A

document.write(a);
// write to the HTML

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Output confirm

A

confirm(“Really?”);
// yes/no dialog
//returns true/false depending on user click

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Output alert

A

alert(a); // output in an alert box

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Comments

A

/* Multi line comment */
// One line

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Loop for
i: i*3, from 0 to 9

example:
‘0: 0’
‘1: 3’
‘2: 6’
‘3: 9’

A

for (var i = 0; i < 10; i++)
{
document.write
(i + “: “ + i*3 + “<br></br>”);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Loop for parsing an array

Toma un arreglo arr y calcula la suma de todos sus elementos.

A

var sum = 0;
for (var i = 0; i < arr.length; i++)
{ sum + = arr[i]; }
// parsing an array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Loop for of
Crea una cadena de texto con el código HTML de una lista no ordenada, donde cada elemento de la lista corresponde a un elemento del array custOrder

A

html = “ “;
for (var i of custOrder) {
html += “<li>” + i + “</li>”;
}

/El bucle for…of solo lee los elementos del array, no los modifica/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

While Loop

Mientras i < 100
i *= 2;

Output:
//’2, ‘ ‘4, ‘ ‘8, ‘ ‘16, ‘ ‘32, ‘ ‘64, ‘ ‘128, ‘ */

A

var i = 1; // initialize
while (i < 100) {
/*enters the cycle if statement is true */
i * = 2;
/ * increment to avoid infinite loop //’2, ‘ ‘4, ‘ ‘8, ‘ ‘16, ‘ ‘32, ‘ ‘64, ‘ ‘128, ‘ * /
document.write(i + “, “);
// output 128 }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Do While Loop

Calcula y muestra las potencias de 2 hasta que se alcanza o supera el número 100.

A

var i = 1; // initialize
do { // enters cycle at least once
i * = 2; // increment to avoid
infinite loop
console.log(i + “, “); // output
} while (i < 100)
// repeats cycle if statement is true at the end
console.log(i);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Break

Imprime los números del 0 al 10, el bucle se detiene cuando i es igual a 5
Por lo tanto imprime hasta el 4

A

for (var i = 0; i < 10; i++) {
if (i == 5) {
break;
} // stops and exits the cycle

document.write(i + “, “);
// last output number is 4 }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Continue.
Este código itera de 0 a 9 e imprime cada número, excepto el 5, separándolos con una coma.

A

for (var i = 0; i < 10; i++) {
if (i == 5) {
continue;
} // skips the rest of the cycle
document.write(i + “, “);
// skips 5
}

15
Q

Objects
Example: student
Incluye la declaracion y el uso
El objeto tiene una función por dentro

A

var student = { // object name
// list of properties and values
firstName:”Jane”,
lastName:”Doe”,
age:18,
height:170,
fullName : function( ) {
// object function
return this.firstName + “ “ + this.lastName;
}
};

student.age = 19;
// setting value
student[age]++;
// incrementing
name = student.fullName();
// call object function

15
Q

DOM Node Properties
attributes

A

Gets a live list of all the characteristics associated with an element.

16
Q

DOM Node Properties

baseURI

A

Returns an HTML element’s absolute base URL.

16
Q
A