Cannot understand this Javascript NaN practice problem. Please help in explaining it

Question:

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.

Example: parseFirstInt(‘No. 10’) should return 10 and parseFirstInt(‘Babylon’) should return NaN.






Answer:

function parseFirstInt(input) {

let inputToParse = input;

for (let i = 0; i < input.length; i++) {

let firstInt = parseInt(inputToParse);

if (!Number.isNaN(firstInt)) {

return firstInt;

}

inputToParse = inputToParse.substr(1);

}

return NaN;

};

<br>
<br>
<br>

Explanation:

> we declare a function ‘parseFirstInt’

> it has 1 parameter ‘input’

> we declare a variable ‘inputToParse’

> we initialize it with a value of the input string

> we use a ‘for loop’ to loop over the elements of the input string

> we then declare another variable inside the ‘for loop’ which is ‘firstInt’

> we initialize its value with the output we get when we run the parseInt method on variable ‘inputToParse’ and store them in the firstInt variable

> we then open an if else statement

> our condition is plain & simple

> if value of ‘firstInt’ is NaN then we want it to exit the loop and return NaN

> if value of ‘firstInt’ is not NaN then we want it to execute the block of code in the if / else loop

> we keep checking for the first integer by using the loop

--------------------stopped understanding from here--------------------




Please explain if possible as to how the loop works. This code is correct and it works.

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

Here is a properly formatted version of the code to make it easier to read:

function parseFirstInt(input) {
  let inputToParse = input;
  for (let i = 0; i < input.length; i++) {
    let firstInt = parseInt(inputToParse);
    if (!Number.isNaN(firstInt)) {
      return firstInt;
    }
    inputToParse = inputToParse.substr(1);
  }
  return NaN;
};
1 Like

Ok… will follow the protocol from next time.

Thanks

This is not correct. I think you misunderstand the if condition. ! is logical negation.

1 Like

I have not understood this solution at all.

I am trying to understand how it works but cannot wrap my head around it.

If you get the time, pls explain it in a simplified manner.

Thanks in advance.

Your understanding up until the line I indicated was correct. Do you have questions about where I said

How can I clarify?

To expand, you are not exiting the loop or anything in this case. You are checking if the value is not NaN. You take absolutely no action from this if statement if the value is NaN.

1 Like

If the value is NaN, then we return NaN right ?

Which means we exit the loop and execute the statement that is after the loop… right ?

No. You do not stop the loop the very first time you find a NaN. You only do what the loop body says. The loop body only has a return for when the parsed input is not NaN. If the entire loop completes without finding a parsed input that is not NaN, then the function returns NaN.

1 Like

Then the loop will do a second iteration after executing

    inputToParse = inputToParse.substr(1);
1 Like

The loop body only has a return for when the parsed input is not or IS NaN ?

Well, what does this say?

1 Like

if (!Number.isNaN(NaN)) then the Boolean value returned is false.

This means that (!Number.isNaN(10)) then the Boolean value returned is true and we shall be returned the value of firstInt… right ?

Right. So the only return statement inside of the loop body is inside of this if clause with the condition that firstInt is not a NaN.

1 Like

So the loop ends right after we return the firstInt; then what does the above statement do ?

I understand that it kind of slices the string but I am getting confused with this function.

Well, how familiar are you with the substr method? From its name alone, I’d guess it takes a substring out of inputToParse. (That’s exactly what it does, but what does 1 mean?)

The substr() method returns a portion of the string, starting at the specified index and extending for a given number of characters afterwards.

const str = 'Mozilla';

console.log(str.substr(1, 2));
// expected output: "oz"

console.log(str.substr(2));
// expected output: "zilla"
1 Like

Can you tell me this:

is the ‘if’ statement in this function without an ‘else’ statement ?

Yes. There is absolutely no else statement in this code.

1 Like

for eg if I passed this assertion:

parseFirstInt (‘Javascript 101’)

The function would do the following:

Step 1: inputToParse = ‘Javascript 101’

Step 2: Start the Loop

Step 3: Parse ‘Javascript 101’ and store it in variable ‘firstInt’

Is this correct ?

Right. And that parse should result in a NaN, right? So what happens then?

1 Like