Home > Back-end >  How do I join this numbers together?
How do I join this numbers together?

Time:09-18

function expandedForm(num) {
    let len = num.toString().length;
    let n = num.toString().split("");
    let result = "";

    for (let i = 0; i < len; i  ) {
        result  = n[i]   "0".repeat(len -1 -i).join("   ");
    }

    return result;
}

What I am trying to do is to separate numbers like this:

1220 = "1000 200 20"

221 = "200 20 1"

I have written the code (not the perfect one) where it gets me all the necessary values but I struggle with joining them together with " ". I tried using .join() but it did not work.

CodePudding user response:

.join works on arrays only

function expandedForm(num) {
    let len = num.toString().length;
    let n = num.toString().split("");
    let result = "";
    let arr=[];
    
    for (let i = 0; i < len; i  ) {
        arr[i] = n[i]   '0'.repeat(len-1-i);
        console.log(arr[i]);
    }
    
    let ans=arr.join(' ');
    return ans;
}

console.log(expandedForm(1220))

CodePudding user response:

Join works with an array, not string. It stringifies two subsequent indexes for all indexes and you can decide what to add between them.

    function expandedForm(num) { // num = 321
        let len = num.toString().length;  // len = 3
        let n = num.toString().split(""); // [3,2,1]
        let result = [];
    
    
        for (let i = 0; i < len; i  ) {
            result.push(n[i]   "0".repeat(len -1 -i));  // pushing till result = ['300','20','10'] 
        }
       
    
        return num   ' = '   result.join('   '); 
      // connection result[0]   '   ' result[1]   '   ' result[2] 
    }
   expandedForm(321); // output: "321 = 300   20   1"

CodePudding user response:

Here's one way of doing it

let num = 221;
function expandedForm(num) {
    let len = num.toString().length;
    let n = num.toString().split("");
    let result = "";

    for (let i = 0; i < len; i  ) {
        let t = "0"
        t = t.repeat(len-1-i)
        if(result.length > 0){
          n[i] !== '0'? result  = ' '  n[i]   t : result 
        } else {
          n[i] !== '0'? result  = n[i]   t : result 
        }
    }
    return result;
}

console.log(expandedForm(2200))
console.log(expandedForm(num))

CodePudding user response:

below would be my approach in a more mathimatical but clean code that you can adjust to your needs.

    let result = parseInt(num / 1000);
    return result ;
}

function x100( num ) {
    num = num % 1000;

    let result = parseInt( num / 100);
    return result;
}

function x10(num ) {
    num = num % 1000;
    num = num % 100;
    let result = parseInt(num /10);
    return result;
}

function x1( num ) {
    num = num % 1000;
    num = num % 100;
    num = num % 10;
    return num
}

num = 12150
console.log(num = `1000 x ${x1000(num)}, 100 x ${x100(num)}, 10 x ${x10(num)}`)```

CodePudding user response:

Although there are a variety of approaches, here are some general tips for you:

  • Probably don't want to output a 0 term unless the input number is exactly 0 (only a leading 0 term is relevant, because it will be the only such term)
  • str.split('') can also be [...str]
  • No need to split a string into an array to access a character str.split('')[0] can also be just str[0]
  • Might want to assert that num is a whole number.
  • Make sure you provide enough test cases in your question to fully define the behaviour of your function. (How to handle trailing zeros, interstitial zeros, leading zeros, etc. Whether the input can be a string.)

function expandedForm(num) {
  const s = num.toString();
  const n = s.length - 1;
  const result = [...s]
    .map((char, index) => char   '0'.repeat(n - index))
    .filter((str, index) => !index ||  str)
    .join('   ');

  return result;
}

console.log(expandedForm(1220));
console.log(expandedForm(221));
console.log(expandedForm(10203));
console.log(expandedForm(0));
console.log(expandedForm(2n**64n));

  • Related