Skip to content

Instantly share code, notes, and snippets.

@enzodanjour
Created April 7, 2022 02:42
Show Gist options
  • Select an option

  • Save enzodanjour/4a2b66437ad92009a9cc386873b79c54 to your computer and use it in GitHub Desktop.

Select an option

Save enzodanjour/4a2b66437ad92009a9cc386873b79c54 to your computer and use it in GitHub Desktop.
inversor letras js
class Inversor {
constructor(letra) {
this.maxSize = letra.length;
this.letra = letra;
this.topo = 0;
this.dados = [];
}
push(dado) {
if (!this.isFull()) {
this.dados[this.topo] = dado;
this.topo++;
} else {
throw new Error("Stack overflow");
}
}
pop() {
if(!this.isEmpty()){
this.topo --;
return this.dados[this.topo];
}else{
throw new Error("Stack underflow");
}
}
isFull() {
if (this.length() === this.maxSize) {
return true;
}
return false;
}
isEmpty(){
return this.length() === 0;
}
length() {
return this.topo;
}
invertePalavra() {
var arrayAdicional = [];
for(var i = 0;i<this.maxSize;i++){
this.push(this.letra[i]);
}
for(var i = this.maxSize;i>0;i--){
arrayAdicional.push(this.pop())
}
return arrayAdicional.join('');
}
}
export default Inversor;
import Inversor from "../src/atividades/inversor";
let p;
beforeEach(
() => {
p = new Inversor('casa');
}
)
test('Tamanho eh',
() => {
expect(p.maxSize).toBe(4);
}
)
test('adicionando letras', ()=>{
var result = p.invertePalavra();
expect(p.dados[0]).toBe("c")
expect(p.dados[3]).toBe("a")
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment