Skip to content

Commit

Permalink
add fourth exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
osiux committed Jun 9, 2020
1 parent 428a4c2 commit 77128d2
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Cada ejercicio tiene las instrucciones de lo que hay que realizar en el Readme d
- [Flexbox](exercises/flexbox/)
- [Flexbox Layout](exercises/flexbox-layout/)
- [Bootstrap](exercises/bootstrap/)
- [Cadenas en Javascript](exercises/js-strings/)

## ¿Por qué?

Expand Down
12 changes: 12 additions & 0 deletions exercises/js-strings/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Cadenas en Javascript

Para este ejercicio solo necesitas modificar el archivo `js/main.js`. Si abres el `index.html`en el navegador, puedes dar click en el botón para verificar el resultado del ejercicio ahi mismo.

## Tareas

1. Crear una variable llamada `myString` cuyo valor sea "Salem es el mejor gato del mundo." (sin comillas).
2. Crear una variable llamada `stringLength` cuyo valor sea el total de caracteres en la cadena `myString`.
3. Crear una variable llamada `catIsAtPosition` cuyo valor sea la posición de la palabra "gato" en la cadena `myString`.
4. Crear una variable llamada `myNewString` cuyo valor sea el mismo que `myString`, pero con la palabra "gato" reemplazada por "michi".

Documentación de cadenas: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String
51 changes: 51 additions & 0 deletions exercises/js-strings/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>JS Strings</title>
</head>
<body>
<button id="btn">Verificar</button>
<script src="js/main.js"></script>
<script>
let task = 1;
const check = (value, name, expected) => {
if (typeof value === "undefined" || value !== expected) {
throw new Error(
`Tarea ${task} incompleta: El valor de "${name}" es incorrecto.`,
);
}
task++;
};
document.getElementById("btn").addEventListener("click", () => {
try {
check(
myString,
"myString",
"Salem es el mejor gato del mundo.",
);
check(stringLength, "stringLength", 33);
check(catIsAtPosition, "catIsAtPosition", 18);
check(
myNewString,
"myNewString",
"Salem es el mejor michi del mundo.",
);

alert("Todo correcto!");
} catch ({ message }) {
if (message.indexOf("is not defined") !== -1) {
const [varName] = message.split(" ", 1);
alert(
`Tarea ${task} incompleta: Variable "${varName}" no está definida.`,
);
} else {
alert(message);
}
}
task = 1;
});
</script>
</body>
</html>
Empty file added exercises/js-strings/js/main.js
Empty file.

0 comments on commit 77128d2

Please sign in to comment.