Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

118
Views
method utakmicaToString() instead of returning string with values of vratiUtakmicu() it is setting result again and then returns

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())
about 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!