Diferència entre revisions de la pàgina «Solucio classe text»
De wikiserver
Línia 12: | Línia 12: | ||
get contarVocales() { | get contarVocales() { | ||
− | let cont = 0; | + | |
+ | let cont = 0; | ||
+ | this.cadena.split("").filter((i) => { | ||
+ | |||
+ | if (i == 'a' || i == 'e' || i == 'i' || i == 'o' || i == 'u') { | ||
+ | cont++; | ||
+ | } | ||
+ | |||
+ | }); | ||
+ | |||
+ | return cont; | ||
+ | |||
+ | |||
+ | /* let cont = 0; | ||
for (var i = this.cadena.length - 1; i >= 0; i--) { | for (var i = this.cadena.length - 1; i >= 0; i--) { | ||
if (this.cadena[i] == 'a' || this.cadena[i] == 'e' || this.cadena[i] == 'i' || this.cadena[i] == 'o' || this.cadena[i] == 'u') { | if (this.cadena[i] == 'a' || this.cadena[i] == 'e' || this.cadena[i] == 'i' || this.cadena[i] == 'o' || this.cadena[i] == 'u') { | ||
Línia 18: | Línia 31: | ||
} | } | ||
} | } | ||
− | return cont; | + | return cont;*/ |
} | } |
Revisió de 19:26, 30 nov 2020
ECMA 6
class text {
constructor(cadena) {
this.cadena = cadena;
}
set anadirCadena(c) {
this.cadena = this.cadena + c;
}
get contarVocales() {
let cont = 0;
this.cadena.split("").filter((i) => {
if (i == 'a' || i == 'e' || i == 'i' || i == 'o' || i == 'u') {
cont++;
}
});
return cont;
/* let cont = 0;
for (var i = this.cadena.length - 1; i >= 0; i--) {
if (this.cadena[i] == 'a' || this.cadena[i] == 'e' || this.cadena[i] == 'i' || this.cadena[i] == 'o' || this.cadena[i] == 'u') {
cont++;
}
}
return cont;*/
}
mostrar() {
console.log(this.cadena);
}
}
var t = new text("Julio Noguera");
t.anadirCadena="profe de m6";
console.log(t.contarVocales);
t.mostrar();
ECMA 5
function text(){
var text= "cadena de caracters";
this.afegirCaracter=function(caracter) //mètode agefirCaracter
{
return text+=""+caracter;
}
this.contarVocals=function() //mètode contarVocals
{
var cont=0;
var texto=text.split("");
for (var i = texto.length - 1; i >= 0; i--) {
if(texto[i]=='a' ||texto[i]=='e'|| texto[i]=='i' ||texto[i]=='o' ||texto[i]=='u'){
cont++;
}
};
return cont;
}
this.mostrarText=function() //mètode mostrarText
{
console.log(text);
}
}//tanquem la classe
var texto= new text();
texto.afegirCaracter("a");
texto.contarVocals();
texto.mostrarText();
AMB PROTOTYPE
function Text(num) {
this.num=num;
}
Text.prototype.cadena=[this.num]; //creamos el array cadena
Text.prototype.afegirCadena=function(nombre){
var nom=nombre.split("");
for(i=0;i<this.num;i++){
this.cadena[i]=nom[i];
}
this.cadena.join("");
console.log(this.cadena);
}
Text.prototype.contarVocal=function(){
var cont=0;
for(i=0;i<this.num;i++){
if(this.cadena[i]== 'a' || this.cadena[i]== 'e' || this.cadena[i]== 'i' || this.cadena[i]== 'o'|| this.cadena[i]== 'u'){
cont++;
}
}
console.log(cont);
}
Text.prototype.mostrar=function(){
var texto="";
for(i=0;i<this.num;i++){
texto+=this.cadena[i];
}
console.log(texto);
}
var texto=new Text(10);
texto.afegirCadena("hola");
texto.contarVocal();