Thursday, May 6, 2021

Javascript Practice 37: JS Hero - parseInt

 
Question: 

Write a function add that takes a string with a summation task and returns its result as a number. Two natural numbers should be added. The summation task is a string of the form '102+17'.

Example: add('102+17') should return 119.




Answer: 

function add (string) { 

let firstNumber = parseInt(string); 
let plusSignPosition = string.indexOf('+'); 
let extractSecondNumber = string.substr(plusSignPosition + 1); 
let secondNumber = parseInt(extractSecondNumber);
let totalValue = firstNumber + secondNumber; 
return totalValue; 

}; 

> let us understand what we are getting in a string for this question
> a summation expression in a string object
> for eg. '2+3' or '256+351', '15985 + 65', etc
        -check in the console by simply typing typeof '65+9' and pressing 'enter'
> these are not numbers but they are strings as they are wrapped in quotes ' ' 
> the summation task could have completed by itself if they were numbers 
        -check in the console by simply typing 65+9 and pressing 'enter'
> the summation task cannot complete by itself as they are strings
         -check in the console by simply typing '65+9' and pressing 'enter', don't forget the quotes
> in order to add the numbers in the string we will need the two numbers and simply add them
> we do this by first finding the first number in the string
> then we find the index position of the plus sign in the string
> based on the position of the plus sign, we extract the number that is placed after the plus sign
> after retrieving both the numbers, we add them to get our result
> so our first step is to find the first number in the string and convert it to a number 
>>> we do that by declaring a variable firstNumber
>>> we use the function parseInt
>>> parseInt function parses a string argument and returns an integer
>>> if the parseInt function encounters a character that is not a number then it ignores it and all the succeeding characters and only returns the integer value parsed up to that point
>>> so if we use parseInt function in the case of '100+5' our return shall be 100 only
        -check in console by typing parseInt('100+5') and pressing 'enter', don't forget the quotes
>>> parseInt not only accessed all the characters which are numbers before the plus sign 
>>> but also returned only the numbers in those characters
>>> and more importantly it converted the returned numbers from characters of a string into  integers that could be used to perform arithmetic operations on literals
> after finding our first number we come to the second step of finding the position of the plus sign 
>>> we declare a variable plusSignPosition
>>> we use the indexOf method to find the position of the '+' sign in the string
>>> we store the returned value in the plusSignPosition variable
> based on the returned value we get, of the position of the plus sign, which is stored in the variable plusSignPosition, we can move to the third step which involves extracting the part of a string, in which we are interested and that is the extractSecondNumber
>>> we declare a variable extractSecondNumber
>>> we use the substr method that extracts part of a string
>>> syntax of substr: string.substr(start, length)
>>> providing the length is optional and we shall not be providing it as we know that our strings are in the form of summation tasks of two natural numbers only  
>>> we provide the starting point from where we need the extraction to happen from the string
>>> our starting point is the plusSignPosition 
>>> however we do not want the plus sign to be extracted with the numbers 
>>> we only need the numbers
>>> by providing just the plusSignPosition as our starting point we shall get the plus sign returned as well because the extraction happens from the starting point itself
>>> in order to omit the plus sign we add +1 to plusSignPosition
>>> like this string.substr(plusSignPosition + 1)
>>> the returned value is stored in the variable extractSecondNumber
>>> these values are not numbers but are strings 
>>> as we have just extracted them and not parsed them 
> in order for us to perform arithmetic operations on numbers / integers / literals we need to convert extractSecondNumber which is a string to a number
>>> we do the conversion by first declaring a variable secondNumber
>>> we use the function parseInt to convert the string to a number
>>> like this: parseInt(extractSecondNumber)
> finally we declare a variable totalValue that simply adds the two variables
>>> firstNumber + secondNumber
>>> the result is stored in the variable totalValue which in the end we return

No comments:

Post a Comment