Seeking assistance in Typescript (javascript) to ensure that the code inside the for loop completes execution before returning
I have a text box where users input strings, and I'm searching for numbers following '#'. I've created a function to find the starting index of '#' and now need to extract only the numbers after '#'. Currently, I am using a for loop to iterate through the string to identify the number value. However, it seems that the return statement is executed prior to the completion of the for loop. How can I adjust this so that the return waits for the for loop to finish and update the internal variable before proceeding?...
readNumber(text: string): number {
const start = text.indexOf('#') + 1;
let newText = '';
for (let index = start; index < text.length; index++) {
if (text.slice(index, 1) === ' ') {
newText = text.slice(start, index - start);
}
}
return +newText;
}
If the user enters "employee #56 cv," I expect the output to be 56.