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

If we are simply parsing the entire string and checking if it is NaN or not, then what does the loop do ?

Well, what happens when the parse results in a NaN? The body of the if statement does not run and then…

What does the substr method do with an argument of 1?

1 Like

we return NaN and exit the loop…right

There is absolutely nothing in the loop body that says ‘exit on a NaN’ though.

function parseFirstInt(input) {
  let inputToParse = input;
  for (let i = 0; i < input.length; i++) {
    // First we try to parse the current input
    let firstInt = parseInt(inputToParse);
    // Then we check if the parse worked (not NaN)
    if (!Number.isNaN(firstInt)) {
      return firstInt;
    }
    // Then we ????
    inputToParse = inputToParse.substr(1);
  }
  return NaN;
};
1 Like

It returns a portion of the string from the index 1 and extending up to the last index.

Exactly! So after you try to parse "Javascript 101" and it fails, then the inputToParse is reduced to "avascript 101" and the loop runs again.

function parseFirstInt(input) {
  let inputToParse = input;
  for (let i = 0; i < input.length; i++) {
    console.log("Iteration: " + i);
    console.log("Current input: " + inputToParse);
    // First we try to parse the current input
    let firstInt = parseInt(inputToParse);
    // Then we check if the parse worked (not NaN)
    if (!Number.isNaN(firstInt)) {
      return firstInt;
    }
    // Then we reduce the inputToParse
    inputToParse = inputToParse.substr(1);
  }
  return NaN;
};

let thisNumber = parseFirstInt("Javascript 101");
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’

Step 4: Check if ‘Javascript 101’ is NaN - true or false

Step 5 (if true): return firstInt

Step 6 (if false): substr() method will remove J from ‘Javascript 101’ and make it ‘avascript 101’

Step 7: again it will parseInt ‘avascript 101’

Step 8: it will check if ‘avascript 101’ is NaN

Am I correct till here ?

Yep. And the loop will keep trying until the stopping condition is met.

1 Like

so it will keep iterating till the integer 101 is stored in the firstInt variable and once the stopping condition is met, which is ’ i < input.length ’ then depending upon the ’ if ’ condition we shall be either returned the firstInt (which in our example is 101) or return NaN… correct

Right. We only return NaN once we run out of characters in our input to try to parse.

1 Like

Ok… JeremyLT

Thank you for helping me with this.

Salutations !!

1 Like
function parseFirstInt(str) {
  /* STEP 1:make the string into an array so that we can employ ES6 array methods (just to make life easier) */
  const step1 = str.split('');

  /* STEP 2:  Now we can use the map method (which lets us transform array elements). Here I use map to change each element into a type Number. */
  const step2 = step1.map(Number);
  /* The above method could be written in many different ways, which all achieve the same thing, such as:
- step1.map(item=>item*1)
or,
- step1.map(item=> Number(item))
or, 
- step1.map(item=> parseInt(item))
or,
- step1.map(item => item ++)
*/

  /* STEP 3: If you print the variable step2, you will see that
  step2 = [
  NaN, NaN, NaN, NaN, NaN, NaN,
  NaN, NaN, NaN, NaN, NaN,   8,
  NaN, NaN, NaN, NaN, NaN, NaN,
  NaN,   4
]

This iswhat I want because now we can use a method to find the first actual number (8). I'm going to use the 'find' method because it allways returns the first instance of a match, as opposed to other methods that keep going or doing stuff.
   */

  /* STEP 3: find the first number */

  return step2.find((i) => i);
}

const input = 'hfdjahfjhsa8fadfads4';

console.log(parseFirstInt(input));

/* One more thing. You can combine all the steps above into one terse statement as follows below. This isn't necessarily better, but it does look cool.
 */

const aShorterVersion = (str) => {
  return str
    .split('')
    .map(Number)
    .find((i) => i);
};

console.log(aShorterVersion(input));

/*