I have following code.
Method vratiUtakmicu() is returning object like this:
{
nameOfFirstTeam: some string,
goalsFirstTeam: random number,
goalsSecoundTeam: random number,
nameOfSecoundTeam: some string
}
But when I call utakmicaToString() method it doesn't take value of goalsFirstTeam,goalsSecoundTeam
Instead looks like it's creating new random score and then print in console.
Copy my code and run so you will see the difference.
Can somebody explain to me why is this happening?
How can I fix this so method utakmicaToString() will take values vratiUtakmicu(), and not creating random result again?
const arrEkipe = ["Srbija" , "Italija" , "Makedonija", "Bugarsk"];
class Utakmica{
constructor(ekipa1,ekipa2){
this.ekipa1 = ekipa1;
this.ekipa2 = ekipa2;
}
rezultat = () =>{
return Math.ceil(Math.random() * 6 );
}
vratiUtakmicu = () => {
return {domacin: this.ekipa1 , domaciGolovi: this.rezultat() ,
gostGolovi: this.rezultat(), gost: this.ekipa2}
}
utakmicaToString = () => {
return this.vratiUtakmicu().domacin + " " +
this.vratiUtakmicu().domaciGolovi + " : " +
this.vratiUtakmicu().gostGolovi + " " +
this.vratiUtakmicu().gost + " " ;
}
}
var Kolo1 = {};
for(let i = 0; i < arrEkipe.length;i+=2){
Kolo1["Utakmica" + i] = new Utakmica(arrEkipe[i],arrEkipe[i+1]);
}
console.log("The result form this console has to be the same")
console.log(Kolo1.Utakmica0.vratiUtakmicu())
console.log("like result from this console but it looks like utakmicaToString() sets new
random result")
console.log(Kolo1.Utakmica0.utakmicaToString())