Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
Possible combinations and convert into alphabet algorithm in JavaScript
Suppose we are given the mapping a = 1, b = 2, ... z = 26, and an encoded message. We are required to write a JavaScript function that takes in the message.
The function should count the number of ways it can be decoded.
For example, the message '111' would give 3, since it could be decoded as 'aaa, 'ka', and 'ak'.
Example
The code for this will be −
const waysToProcess = ( message, ways = 0 ) => {
if ( message.length ) {
ways = waysToProcess( message.slice( 1 ,message.length), ways );
const numCurr = parseInt( message[0] );
const numNext = "undefined" === typeof message[1] ? null :
parseInt(message[1]);
if ( numCurr && numNext
&& numCurr Output
And the output in the console will be −
3
Advertisements
