Currently, I am in the process of learning Angular and have encountered an issue while working on an assignment. The error message that I am facing is "Cannot read properties of undefined (reading 'push')." Despite knowing that this is a common error, I am struggling to identify what I am doing wrong. I am hopeful that someone here can offer me some assistance.
In my assignment, I am attempting to separate even numbers into the "even" array and odd numbers into the "unEven" array. The problem arises when trying to push these numbers into their respective arrays. I have already declared and initialized them as empty arrays. However, I am uncertain about what else I can do to resolve this issue...
Below is a snippet from my .ts file:
import { Component, Input, OnInit, Output } from '@angular/core';
@Component({
selector: 'app-game-control',
templateUrl: './game-control.component.html',
styleUrl: './game-control.component.css'
})
export class GameControlComponent implements OnInit{
counter = 0;
intervalID;
even: number[]=[];
unEven: number[]=[];
onStartGame(){
this.intervalID = setInterval(this.GameStarted,1000)
}
onStopGame(){
clearInterval(this.intervalID)
}
GameStarted(){
this.counter = this.counter +1;
Number.isInteger(this.counter/2)? this.even.push(this.counter) : this.unEven.push(this.counter);
console.log('Even numbers logged: '+ this.even);
console.log('Odd numbers logged: '+ this.unEven);
}
ngOnInit(){}
}