Autor: Juan Pablo Fuentes
Formador de programación y bases de datos
Examen javascript
Tenéis dos horas para hacerlo. Suerte.
Ejemplo examen
function pin(){
var res="";
for (var i=0;i<4;i++){
res+=Math.floor(Math.random()*10);
}
return res;
}
function sumaPares(tabla){
var suma=0;
for (var i=0;i<tabla.length;i++){
if (tabla[i]%2===0){
suma+=tabla[i];
}
}
return suma;
}
Ejercicio parientes funcionalizado
$(function () {
function creaBoton(clase, valor) {
return '<input type="button" class="btn btn-success ' + clase + '" value="' + valor + '">';
}
function lista(elementos, valor) {
var res = "<ul>";
for (var i = 0; i < elementos; i++) {
res += '<li>' + valor + '</li>';
}
return res + "</ul>";
}
function columna(ancho, elementos) {
var columna = '<div class="col-sm-' + ancho + '">';
columna += creaBoton('menos', '-');
columna += creaBoton('mas', '+');
columna += creaBoton('otros', '/\\');
columna += creaBoton('otrosmenos', '\\/');
columna += lista(elementos, "-");
columna += "</div>";
return columna;
}
function columnas(ncol, ancho, elementos) {
var res = "";
for (var i = 0; i < ncol; i++) {
res += columna(ancho, elementos);
}
return res;
}
$('#nuevo').click(function () {
$('#resultado').html(columnas(4, 3, 5));
$('.menos').click(function () {
// $(this).siblings('ul').children().eq(0) .remove();
//Buscamos los hermanos de tipo 'ul' y quitamos el último hijo
$(this).siblings('ul').children().last().remove();
//Si ya no hay más hijos deshabilitamos el botón
if ($(this).siblings('ul').children().length === 0) {
$(this).attr('disabled', 'disabled');
}
});
$('.mas').click(function () {
//Buscamos el hermano de tipo ul y añadimos un elemento
$(this).siblings('ul').append('<li>-</li>');
$(this).siblings('.menos').removeAttr('disabled');
});
$('.otros').click(function () {
//Los hermanos de mi columna tienen listas 'ul' y añadimos un elemento
$(this).parent().siblings().children('ul').append('<li>-</li>');
$(this).parent().siblings().children('.menos').removeAttr('disabled');
});
$('.otrosmenos').click(function () {
//Los hermanos de mi columna tienen listas 'ul' y añadimos un elemento
$(this).parent().siblings().children('ul').each(function () {
$(this).children().last().remove();
if ($(this).children().length === 0) {
$(this).siblings('.menos').attr('disabled', 'disabled');
}
});
});
});
});
Uso correcto e incorrecto funciones
function calcular() {
var x = document.getElementById("num1").value;
var y = document.getElementById("num2").value;
var gx = x;
var gy = y;
var w;
while (y != 0) {
w = x % y;
x = y;
y = w;
}
var mcm = gx * gy / x;
document.getElementById("resultado").value = mcm;
}
function calcularBien() {
var x = document.getElementById("num1").value;
var y = document.getElementById("num2").value;
document.getElementById("resultado").value = minimoComunMultiplo(x, y);
}
function minimoComunMultiplo(x, y) {
var gx = x;
var gy = y;
var w;
while (y != 0) {
w = x % y;
x = y;
y = w;
}
return gx * gy / x;
}
var frase="Juanito";
console.log(alReves('Viva la Pepa'));
function alReves(frase){
return frase.split('').reverse().join('');
}
Plugins para jQuery
http://tutorialzine.com/2013/04/50-amazing-jquery-plugins/
http://www.creativebloq.com/jquery/top-jquery-plugins-6133175
https://designmodo.com/jquery-ui-framework-plugins/
Webs alumnos
Ejercicio tablas completo
$(function () {
function celdas(){
$('td').mouseenter(function () {
$(this).addClass("destacado");
});
$('td').mouseleave(function () {
$(this).removeClass("destacado");
});
}
$('#crear').click(function () {
var tabla = "| # | "; } tabla += "
| # | "; } for (var i = 0; i < filas; i++) { tabla += "