Messing around with reversing a number from an Angular input.
Here is the method and property:
reversedIntInput = "";
reverseInt(event: any) {
const inputString = (<HTMLInputElement>event.target).value;
const parsed = parseInt(inputString);
if (Number.isNaN(parsed) || inputString == "" || inputString === null) {
this.reversedIntInput = "Nice try but this is not a number.";
} else {
this.reversedIntInput = inputString
.split("")
.reverse()
.join("");
}
}
and here is the html:
<label>Reverse an Integer</label>
<input type="text" class="form-control" (input)="reverseInt($event)">
<p>{{ reversedIntInput }}</p>
If I type "d" in the input, it does indeed display "Nice try but this is not a number." if I type "45" in the input it does indeed display "54".
But if I type "54ddd" it just reverses it as a string and displays "ddd45" when it should display "Nice try but this is not a number."
Why is that? I console log both inputString
and parsed
and inputString continues to update when a letter is typed but parsed does not.