Diferència entre revisions de la pàgina «Solucio classe text»
De wikiserver
Línia 1: | Línia 1: | ||
+ | <source lang="java"> | ||
+ | class text { | ||
+ | constructor(cadena) { | ||
+ | this.cadena = cadena; | ||
+ | } | ||
+ | |||
+ | set anadirCadena(c) { | ||
+ | |||
+ | this.cadena = this.cadena + c; | ||
+ | } | ||
+ | |||
+ | get contarVocales() { | ||
+ | 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(); | ||
+ | |||
+ | |||
+ | </source> | ||
+ | |||
<source lang="java"> | <source lang="java"> | ||
Revisió del 15:14, 17 oct 2017
class text {
constructor(cadena) {
this.cadena = cadena;
}
set anadirCadena(c) {
this.cadena = this.cadena + c;
}
get contarVocales() {
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();
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();