JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<h2>Se scrie un sir(cuvant) in ordine inversa</h2>
<input type ='text' id='src'>
<input type = "button" id='apasaBtn' value = "INVERSEAZA SIRUL">
<!-- added by lightpoint -->
<div id='rezultat'> </div>
CSS
.stilInit {
font-family : Courier New;
font-style: regular;
/*alte chestii aici daca este nevoie*/
}
.stilEroare {
font-size: 32px;
font-weight: bold;
color : red;
}
.stilOk {
font-size: 25px;
font-weight: bold;
color : green;
font-family: Verdana;
font-style: italic;
}
JavaScript
//constructor
var Oglinda = function(sursaID, destinatieID) {
this.sursaID = sursaID;
this.destinatieID = destinatieID;
}
//proto
Oglinda.prototype = {
init: function() {
$(this.destinatieID).addClass('stilInit');
return this;
},
executa: function() {
var valoare = this.sursaID.val();
var lungimeSir = valoare.length;
lungimeSir === 0 ? this.seteazaStilEroare() : this.seteazaStilOk();
$(this.destinatieID).html(lungimeSir === 0 ? "EROARE: sirul este vid!" : this.procesare(valoare));
return this;
},
procesare: function(x) {
var oglx="";
for (var i = x.length - 1; i >= 0; i--) {
oglx = oglx.concat(x[i]);
}
return oglx;
},
seteazaStilEroare: function() {
if ($(this.destinatieID).hasClass('stilOk')) {
$(this.destinatieID).removeClass('stilOk');
}
$(this.destinatieID).addClass('stilEroare');
},
seteazaStilOk: function() {
if ($(this.destinatieID).hasClass('stilEroare')) {
$(this.destinatieID).removeClass('stilEroare');
}
$(this.destinatieID).addClass('stilOk');
}
}
$(document).ready(function() {
var oglindaMea = new Oglinda($('#src'), $('#rezultat'));
$('#apasaBtn').click(function() {
oglindaMea.init().executa();
});
});