Solucio Yahoo rellotge
De wikiserver
SOLUCIÓ YAHOO
function Yahoo(){
var numeros = "";
this.seguent = function(numeros){
switch(numeros){
case "cero":
return "un";
break;
case "un":
return "dos";
break;
case "dos":
return "tres";
break;
case "tres":
return "quatre";
break;
case "quatre":
return "molts";
break;
case "molts":
return "molts";
break;
case "desconegut":
return "desconegut";
break;
default:
return "desconegut";
}//cerrar switch
}; //cerrar this.seguent
this.suma = function(){
var numero = "";
var x = "";
if(arguments.length==0){
return "desconegut";
}
else if(arguments.length==1){
return arguments[0];
}
else{
numero = arguments[0];
x = arguments[1];
switch(numero){
case "cero":
return this.suma(x);
break;
case "un":
return this.seguent(x);
break;
case "dos":
return this.seguent(this.seguent(x));
break;
case "tres":
return this.seguent(this.seguent(this.seguent(x)));
break;
case "quatre":
return this.seguent(this.seguent(this.seguent(this.seguent(x))));
break;
case "molts":
return "molts";
break;
case "desconegut":
return "desconegut";
break;
default:
return "desconegut";
}//cerrar switch
} //cerrar else
}; //cerrar this.suma
/*Otra forma de hacerlo es sustituir la función de sumar sobrecargada por esta otra*/
function afegirMetode(objecte, nom, funcio){
var old = objecte[nom];
objecte[nom] = function(){
if(funcio.length == arguments.length)
return funcio.apply(this, arguments);
else if (typeof old == 'function')
return old.apply(this, arguments);
};
}
afegirMetode(this, "suma", function(){
return "desconegut";
});
afegirMetode(this, "suma", function(x){
return arguments[0];
});
afegirMetode(this, "suma", function(numero,x){
switch(numero){
case "cero":
return this.suma(x);
break;
case "un":
return this.seguent(x);
break;
case "dos":
return this.seguent(this.seguent(x));
break;
case "tres":
return this.seguent(this.seguent(this.seguent(x)));
break;
case "quatre":
return this.seguent(this.seguent(this.seguent(this.seguent(x))));
break;
case "molts":
return "molts";
break;
case "desconegut":
return "desconegut";
break;
default:
return "desconegut";
}
});
*/
} //cerrar yahoo
var yahoo1 = new Yahoo();
alert(yahoo1.seguent("desconegut"));
alert(yahoo1.seguent("cero"));
alert(yahoo1.seguent("un"));
alert(yahoo1.seguent("dos"));
alert(yahoo1.seguent("tres"));
alert(yahoo1.seguent("quatre"));
alert(yahoo1.seguent("molts"));
alert(yahoo1.seguent("seis"));
alert(yahoo1.suma());
alert(yahoo1.suma("un"));
alert(yahoo1.suma("cero","dos"));
alert(yahoo1.suma("quatre","dos"));
alert(yahoo1.suma("un","dos"));
alert(yahoo1.suma("desconegut","tres"));
alert(yahoo1.suma("cero","un"));
alert(yahoo1.suma("quatre","cero"));
alert(yahoo1.suma("quatre","un"));
alert(yahoo1.suma("tres","un"));
alert(yahoo1.suma("tres","cero"));
alert(yahoo1.suma("tres","dos"));
alert(yahoo1.suma("un","desconegut"));
SOLUCIO RELLOTGE
function Rellotge() {
var rellotge;
var rellotgeSec;
var tempsFun = 0;
this.ContructorRellotge = function () {
rellotge = new Date();
};
this.GetTime = function () {
rellotgeSec = new Date();
var timeMili = rellotgeSec.getTime() - rellotge.getTime();
var seg = timeMili / 1000;
return tempsFun + seg;
};
this.StopTime = function () {
tempsFun = this.GetTime();
rellotge = null;
};
this.StartTime = function () {
rellotge = new Date();
};
this.setFunctionOnTime = function (time) {
var that = this;
setInterval(function () {
that.hola();
}, time * 1000); //hola es una funció d'exemple
};
this.hola = function () {
alert("hola mundo");
};
}
var rellotge = new Rellotge();
rellotge.ContructorRellotge();
alert(rellotge.GetTime());
rellotge.setFunctionOnTime(2);
/**OTRA FORMA*/
function Rellotge() {
var rellotge;
var rellotgeSec;
var tempsFun = 0;
var parar;
this.ContructorRellotge = function () {
rellotge = new Date();
};
this.GetTime = function () {
rellotgeSec = new Date();
var timeMili = rellotgeSec.getTime() - rellotge.getTime();
var seg = timeMili / 1000;
return tempsFun + seg;
};
this.StopTime = function () {
tempsFun = this.GetTime();
setTimeout(function () {
clearInterval(parar);
rellotge = null; //nos cargamos el reloj haciendo que apunte a null
}, 6000); //a los 6 segundos borra la referencia y pausa el setInterval
};
this.StartTime = function () {
rellotge = new Date();
};
this.setFunctionOnTime = function (time, fun) {
parar = setInterval(fun, time * 1000); //hola es una funció d'exemple
};
}
var rellotge = new Rellotge();
rellotge.ContructorRellotge();
alert(rellotge.GetTime());
let s = function () { alert("hola") };
rellotge.setFunctionOnTime(2, s);
rellotge.StopTime();