domingo, 31 de enero de 2016

Strings

Los Strings en JavaScript son usados para almacenar y manipular textos.
Del contenido del capítulo JS Strings W3Schools.

Ejercicio 1. Este es el código exacto del documento que se muestra:
<!DOCTYPE html>
<html>
<head>
<title>Strings en JavaScript</title>
<meta charset="UTF-8" />
<meta name="author" content="Ángel Puente" />
</head>
<body>
<h2>Podemos usar comillas dobles o sencillas.</h2>
<p id="demo"></p>
<script>
var carName1 = "Volvo XC60";
var carName2 = 'Volvo XC60';
document.getElementById("demo").innerHTML =
carName1 + "<br>" + carName2; 
</script>
</body>
</html>


Ejercicio 2.
Este es el código exacto del documento que se muestra:
<!DOCTYPE html>
<html>
<head>
<title>Strings en JavaScript</title>
<meta charset="UTF-8" />
<meta name="author" content="Ángel Puente" />
</head>
<body>
<h2>Comillas sencillas dentro de dobles o dobles dentro de sencillas.</h2>
<p id="demo"></p>
<script>
var answer1 = "It's alright"; //Un apóstrofo (comilla sencilla) dentro de comillas dobles
var answer2 = "He is called 'Johnny'"; //Comillas sencillas dentro de comillas dobles
var answer3 = 'He is called "Johnny"'; //Comillas dobles dentro de comillas sencillas
document.getElementById("demo").innerHTML =
answer1 + "<br>" + answer2 + "<br>" + answer3;
</script>
</body>
</html>


Ejercicio 3. Este es el código exacto del documento que se muestra:
<!DOCTYPE html>
<html>
<head>
<title>Strings en JavaScript</title>
<meta charset="UTF-8" />
<meta name="author" content="Ángel Puente" />
</head>
<body>
<h2>Longitud del <i>string</i> con la propiedad <i>length</i>.</h2>
<p id="demo"></p>
<script>
var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
document.getElementById("demo").innerHTML = txt.length;
</script>
</body>
</html>


Ejercicio 4. Este es el código exacto del documento que se muestra:
<!DOCTYPE html>
<html>
<head>
<title>Strings en JavaScript</title>
<meta charset="UTF-8" />
<meta name="author" content="Ángel Puente" />
</head>
<body>
<h2>Salvar las comillas con el carácter de escape <i>\</i>.</h2>
<p id="demo"></p>
<script>
var x = 'It\'s alright';
var y = "We are the so-called \"Vikings\" from the north.";
document.getElementById("demo").innerHTML = x + "<br>" + y; 
</script>
</body>
</html>


Ejercicio 5.
Este es el código exacto del documento que se muestra:
<!DOCTYPE html>
<html>
<head>
<title>Strings en JavaScript</title>
<meta charset="UTF-8" />
<meta name="author" content="Ángel Puente" />
</head>
<body>
<h2>Mi página web.</h2>
<p>El mejor modo de romper una línea de código es después de un operador o una coma.</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = //saltamos de línea después del =
"Hola Dolly.";
</script>
</body>
</html>


Ejercicio 6. Este es el código exacto del documento que se muestra:
<!DOCTYPE html>
<html>
<head>
<title>Strings en JavaScript</title>
<meta charset="UTF-8" />
<meta name="author" content="Ángel Puente" />
</head>
<body>
<h2>Mi página web.</h2>
<p>Se puede romper también una línea de código con el carácter \.</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "Hola \ // Saltamos de línea con el carácter de escape
    Dolly.";
</script>
</body>
</html>


Ejercicio 7. Este es el código exacto del documento que se muestra:
<!DOCTYPE html>
<html>
<head>
<title>Strings en JavaScript</title>
<meta charset="UTF-8" />
<meta name="author" content="Ángel Puente" />
</head>
<body>
<h2>Mi página web.</h2>
<p>El mejor método para romper una línea de código es empleando la suma de strings.</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "Hola" +
    "Dolly.";
</script>
</body>
</html>


Ejercicio 8.
Este es el código exacto del documento que se muestra:
<!DOCTYPE html>
<html>
<head>
<title>Strings en JavaScript</title>
<meta charset="UTF-8" />
<meta name="author" content="Ángel Puente" />
</head>
<body>
<h2>Mi página web.</h2>
<p id="demo">No se puede emplear el carácter \ al principio de la línea para saltar a la siguiente.</p>
<script>
document.getElementById("demo").innerHTML = \
"Hola Dolly."; //No se va a mostrar este "Hola Dolly"
</script>
</body>
</html>


Ejercicio 9. Este es el código exacto del documento que se muestra:
<!DOCTYPE html>
<html>
<head>
<title>Strings en JavaScript</title>
<meta charset="UTF-8" />
<meta name="author" content="Ángel Puente" />
</head>
<body>
<h2>Los strings pueden declararse también como objetos.</h2>
<p id="demo"></p>
<script>
var x = "John";              // x es un string
var y = new String("John");  // y es un objeto
document.getElementById("demo").innerHTML =
typeof x + "<br>" + typeof y;
</script>
</body>
</html>


Ejercicio 10. Este es el código exacto del documento que se muestra:
<!DOCTYPE html>
<html>
<head>
<title>Strings en JavaScript</title>
<meta charset="UTF-8" />
<meta name="author" content="Ángel Puente" />
</head>
<body>
<h2>No crear nunca strings como objetos.</h2>
<p>Strings y objetos no pueden compararse correctamente.</p>
<p id="demo"></p>
<script>
var x = "John";              // x is a string
var y = new String("John");  // y is an object
document.getElementById("demo").innerHTML = (x==y); //Devuelve true porque tienen el mismo valor
</script>
</body>
</html>


Ejercicio 11.
Este es el código exacto del documento que se muestra:
<!DOCTYPE html>
<html>
<head>
<title>Strings en JavaScript</title>
<meta charset="UTF-8" />
<meta name="author" content="Ángel Puente" />
</head>
<body>
<h2>No crear nunca strings como objetos.</h2>
<p>Strings y objetos no se pueden comparar con seguridad.</p>
<p id="demo"></p>
<script>
var x = "John";              // x is a string
var y = new String("John");  // y is an object
document.getElementById("demo").innerHTML = (x===y); //Devuelve false porque es un string y un objeto
</script>
</body>
</html>
Ejercicio 12. Este es el código exacto del documento que se muestra:
<!DOCTYPE html>
<html>
<head>
<title>Strings en JavaScript</title>
<meta charset="UTF-8" />
<meta name="author" content="Ángel Puente" />
</head>
<body>
<p>No crear nunca strings como objetos.</p>
<p>Los objetos en JavaScript no se pueden comparar.</p>
<p id="demo"></p>
<script>
var x = new String("John");  // x is an object
var y = new String("John");  // y is an object
document.getElementById("demo").innerHTML = (x==y);
</script>
</body>
</html>

No hay comentarios:

Publicar un comentario