From 619687da2ef0c923a3c8e73774c1a92e8000ffa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juhani=20Krekel=C3=A4?= Date: Tue, 8 Aug 2023 17:13:11 +0300 Subject: [PATCH] =?UTF-8?q?Lis=C3=A4=C3=A4=20.luokat()=20metodi=20LuokkaAs?= =?UTF-8?q?teet-olioon?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tietotyypit.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tietotyypit.js b/tietotyypit.js index 7eb5e3f..92caef7 100644 --- a/tietotyypit.js +++ b/tietotyypit.js @@ -26,6 +26,18 @@ class LuokkaAste { class LuokkaAsteet { asteet = []; + luokat() { + let luokat = []; + for (let aste = 0; aste < this.asteet.length; aste++) { + if (this.asteet[aste] !== undefined) { + for (let luokka of this.asteet[aste].luokat()) { + luokat.push(`${aste}${luokka}`); + } + } + } + return luokat; + } + seuraavaAste() { // Seuraava aste on yksi suurinta listassa jo olevaa astetta isompi, tai // 1 jos listassa ei vielä ole asteita @@ -121,6 +133,7 @@ testi('asteiden lisääminen', () => { assertThrow('3. aste uudelleen', 'luokka-aste 3 on jo olemassa', () => { luokkaAsteet.lisää(3); }); + assertEq('lisättyä', luokkaAsteet.luokat(), ['1A', '2A', '3A']); }); testi('asteiden poistaminen', () => { @@ -132,12 +145,14 @@ testi('asteiden poistaminen', () => { assertThrow('3. asteen poisto', 'luokka-astetta 3 ei ole olemassa', () => { luokkaAsteet.poista(3); }); + assertEq('poistettua', luokkaAsteet.luokat(), []); }); testi('asteiden muuttaminen', () => { let luokkaAsteet = new LuokkaAsteet(); assertEq('lisääminen', luokkaAsteet.lisää(), 1); luokkaAsteet.muuta(1, 5); + assertEq('muutettua', luokkaAsteet.luokat(), ['5A']); assertThrow('poisto', 'luokka-astetta 1 ei ole olemassa', () => { luokkaAsteet.poista(1); });