Skip to content

Instantly share code, notes, and snippets.

@alelazcano
Last active May 24, 2019 00:19
Show Gist options
  • Save alelazcano/3b7d50379f92578543663f0a262b6e92 to your computer and use it in GitHub Desktop.
Save alelazcano/3b7d50379f92578543663f0a262b6e92 to your computer and use it in GitHub Desktop.
Ejemplo muy básico de 'while' / 'for' en Javascript
var alumnos = ['Mariana', 'Sebastián', 'Constanza'];
console.log("===== while =====");
function listarAlumnos (lista) {
var n = 0;
while (n <= lista.length-1) {
console.log(n +" _ "+lista[n]);
n++
}
}
listarAlumnos(alumnos);
console.log("===== for =====");
function listarAlumnos2 (lista) {
for (var n=0; n <= lista.length-1; n++)
console.log(n+1 +" - "+ lista[n]);
}
listarAlumnos2 (alumnos);
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
var alumnos = ['Mariana', 'Sebastián', 'Constanza'];
console.log("===== while =====");
function listarAlumnos (lista) {
var n = 0;
while (n <= lista.length-1) {
console.log(n +" _ "+lista[n]);
n++
}
}
listarAlumnos(alumnos);
console.log("===== for =====");
function listarAlumnos2 (lista) {
for (var n=0; n <= lista.length-1; n++)
console.log(n+1 +" - "+ lista[n]);
}
listarAlumnos2 (alumnos);
</script>
<script id="jsbin-source-javascript" type="text/javascript">var alumnos = ['Mariana', 'Sebastián', 'Constanza'];
console.log("===== while =====");
function listarAlumnos (lista) {
var n = 0;
while (n <= lista.length-1) {
console.log(n +" _ "+lista[n]);
n++
}
}
listarAlumnos(alumnos);
console.log("===== for =====");
function listarAlumnos2 (lista) {
for (var n=0; n <= lista.length-1; n++)
console.log(n+1 +" - "+ lista[n]);
}
listarAlumnos2 (alumnos);</script></body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment