Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Write number in expanded form

// You will be given a number and you will need to return it as a string in Expanded Form. For example:

/* 
	// expandedForm(12); // Should return '10 + 2'
	// expandedForm(42); // Should return '40 + 2'
	// expandedForm(70304); // Should return '70000 + 300 + 4'
*/

const expandedForm = arr => {
    let pow = []
    let decimal = []
    arr = arr.toString().split('')
    let len = arr.length
    for(let i = 0; i <= len - 1; i++){
        pow.unshift(i)
    }
    arr.forEach((x,index) => {
        x = parseInt(x)
        decimal.push(x*10**pow[index])
    })
    let toDecimal = decimal.filter(a => a !== 0)
    return toDecimal.toString().split(',').join(' + ')
};
console.log(expandedForm(70304))

// With love @kouqhar
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript export multiple function 
Javascript :: rc-notification react 
Javascript :: angular 12 features 
Javascript :: react form hook trigger is not a function 
Javascript :: Angular p-dialog 
Javascript :: how to use crypto module in nodejs 
Javascript :: es6 modules node 
Javascript :: koa access request body 
Javascript :: jquery creating several items 
Javascript :: javascript reduce return array 
Javascript :: having written a counter with redux how does it work 
Javascript :: js after settimeout 
Javascript :: add css class to button javascript 
Javascript :: d3.js onclick event 
Javascript :: minecraft fps client that supports linux 
Javascript :: array empty strings 
Javascript :: gatsby change page url 
Javascript :: nodejs remove element from array 
Javascript :: even.target in javascript 
Javascript :: how to disable menu bar in browser using javascript 
Javascript :: sequelize documentation 
Javascript :: update karma jasmine to specific version 
Javascript :: nodejs mysql connection 
Javascript :: window width onload jquery 
Javascript :: javascript for loop 
Javascript :: notify jquery 
Javascript :: side effect, useEffect, return 
Javascript :: express generator error handling 
Javascript :: React S3 Bucket 
Javascript :: how to use react-native-vector-icons 
ADD CONTENT
Topic
Content
Source link
Name
5+1 =