Introduction

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!

Task Statement and Description

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.

Step-by-Step Solution Building: Step 1

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:

Step-by-Step Solution Building: Step 2

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.

Step-by-Step Solution Building: Step 3

Finally, we multiply all the numbers in the numbers array together. The multiplication result gets stored in the result variable.

Full Solution

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

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

Lesson Summary

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!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal