Lesson 2

Welcome! In this unit, we have an exciting and practical task that will test your **JavaScript programming skills**. We will be parsing strings and making type conversions. So, let's dive into it!

Our task for the day involves creating a JavaScript function called `parseAndMultiplyNumbers()`

. This function is designed to accept a string as input. However, it's not just any string — the input we'll consider is a playful mix of numbers and words.

The purpose of this function is to analyze the input string, extract all the numbers, convert these numbers (currently string types) into integer data types, and then multiply all these numbers together. The final output? It's the product of all those numbers!

Here's an illustration for clarification. Given the input string "I have 2 apples and 5 oranges," our function should return the product of 2 and 5, which is 10.

The primary task is to parse the string and identify the numbers. To do that, let's create an empty string, `num`

, to accumulate digits and an array `numbers`

to collect all the numbers we find:

JavaScript`1let inputString = "I have 2 apples and 5 oranges"; 2let num = ""; 3let numbers = [];`

The next step requires iterating through the input string character by character. When we encounter a digit, we append it to our `num`

string. If a character isn’t a digit and `num`

isn’t empty, it means we've reached the end of a number.

At this point, we convert `num`

to an integer, add it to the `numbers`

array, and reset `num`

to an empty string. If the character isn’t a digit and `num`

is empty, we simply skip and progress.

To check if a character is a digit, we use the `isNaN()`

function. The `isNaN()`

function returns `true`

if a value is `NaN`

(Not-a-Number) and `false`

otherwise. However, since we want to know if a character is a digit, we use the negation `!isNaN(ch)`

. This will return `false`

for non-digit characters and `true`

for digits. Additionally, we check that the character isn't a space (`ch !== ' '`

) to avoid spaces being counted as numbers.

JavaScript`1for (let ch of inputString) { 2 if (!isNaN(ch) && ch !== ' ') { // Check if character is a digit 3 num += ch; 4 } else if (num !== "") { 5 numbers.push(parseInt(num)); // Convert num to integer and add it to numbers array 6 num = ""; 7 } 8} 9// After the loop, we must check if 'num' is not empty 10// because it indicates that the last part of the string contains a number. 11if (num !== "") { 12 numbers.push(parseInt(num)); 13} 14console.log(numbers); // Output should be [2, 5]`

Finally, we multiply all the `numbers`

in the `numbers`

array together. The multiplication result gets stored in the `result`

variable.

JavaScript`1let result = 1; 2for (let number of numbers) { 3 result *= number; 4} 5console.log(result); // Output should be 10`

Bringing together all the steps, our final JavaScript solution manifests as follows:

JavaScript`1function parseAndMultiplyNumbers(inputString) { 2 let num = ""; 3 let numbers = []; 4 5 for (let ch of inputString) { 6 if (!isNaN(ch) && ch !== ' ') { // Check if character is a digit 7 num += ch; 8 } else if (num !== "") { 9 numbers.push(parseInt(num)); // Convert num to integer and add it to numbers array 10 num = ""; 11 } 12 } 13 if (num !== "") { 14 numbers.push(parseInt(num)); 15 } 16 17 let result = 1; 18 for (let number of numbers) { 19 result *= number; 20 } 21 return result; 22} 23 24// Call the function 25console.log(parseAndMultiplyNumbers("I have 2 apples and 5 oranges"));`

This solution also caters to numbers situated at the end of the input string.

Applaud yourself! You've successfully developed a JavaScript function that deftly navigates strings to identify numbers, performs a data type conversion, and then conducts an arithmetic operation on those numbers. You've truly demonstrated admirable skill in orchestrating these coding concepts!

However, as always in coding, practice is key to improvement. With this solution, you could try to perform different operations on the numbers or change the condition for identifying valid numbers, thereby further sharpening your JavaScript skills. Here's to coding greatness!