Created
April 7, 2022 02:42
-
-
Save enzodanjour/4a2b66437ad92009a9cc386873b79c54 to your computer and use it in GitHub Desktop.
inversor letras js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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