I have created a random number generator that requires the user to input a maximum and minimum value to generate a random number within that range. The application will show an error under two conditions:
- If either of the numbers entered is negative
- If the maximum number is less than the minimum number
Here is the code snippet for the method that generates the random number:
generateRandomNumber(max, min) {
console.log(max<min);
if(max > min && min > 0){
this.number = Math.floor(Math.random() * (max - min + 1)) + parseInt(min);
this.err_compare = false;
this.err_negative = false;
}
else if(max < 0 || min < 0){
this.err_negative = true;
}
else if(max < min){
this.err_compare = true;
}
}
When I enter max = 10 and min = 1, it returns false. But when I change min to 2, the log returns true.
Could someone explain why this is happening?
UPDATE
In response to Mr @Setu's query, I tested by directly entering the numbers into the function and received the correct result. Thus, I suspect there might be an issue with the input. Here is the form markup:
<form #generator="ngForm">
<ion-item>
<ion-label for="max" floating>Maximum</ion-label>
<ion-input type="number" [(ngModel)]="max" name="max" required></ion-input>
</ion-item>
<ion-item>
<ion-label for="min" floating>Minimum</ion-label>
<ion-input type="number" [(ngModel)]="min" name="min" required></ion-input>
</ion-item>
<br>
<button default ion-button block [disabled]="!generator.valid" (click)="generateRandomNumber(max,min)">Generate</button>
</form>
It seems like the system is expecting me to input
20,21,22...
when I actually want to input just
2