This exercise is from jshero.com.
Write a function parseFirstInt that takes a string and returns the first integer present in the string. If the string does not contain an integer, you should get NaN.
Here is my working solution. I'm posting in hopes of receiving pointers on how I could have written the code more effectively.
function parseFirstInt(str) {
if (Number.isNaN(str) === true) {
return NaN;
} else {
let searchFrom = str.search(/[-+]?[0-9]+/g);
let searched = str.substr(searchFrom);
return parseInt(searched, 10);
};
}
Example: parseFirstInt('No. 10') should return 10 and parseFirstInt('Babylon') should return NaN. parseFirstInt('No. -3) should return -3.
Thank you!