0

I have number 8939, then I want to get the array [8000,7000,6000,5000,4000,3000,2000,1000] and example if I have 340 then I can get the array 300,200,100.

I just knew that , if I user

i = i/1000 * 1000

Then I can round down 8939 to 8000, but I don't know how can I got the result that I want above.

4
  • 2
    What does "get the array of the number" mean? Commented Aug 13, 2013 at 8:03
  • I'm sorry, my bad. I edited. Commented Aug 13, 2013 at 8:03
  • What is your question? Commented Aug 13, 2013 at 8:06
  • 1
    You may want to start with the JavaScript Math.log() function; that gives a logarithmic value that will give you (in simple terms) the number of zeros that you need in each value of your array. Commented Aug 13, 2013 at 8:06

5 Answers 5

3

No reason to complicate it, you can use a for loop

function getArrForNum(num){
   var arr = []; // create a new array
   var digitNum = num.toString().length; //get the number of digits, can use Math.log instead
   var mulOf10 = Math.pow(10,digitNum-1);
   for(var i=1;i*mulOf10 < num;i++){
       arr.push(i*mulOf10); // push the next multiple of 1000
   }
   return arr; //return it
}

Then you can use it:

getArrForNum(3211); //1000,2000,3000
getArrForNum(321); //100,200,300

Here is a one line version just for the challenge, but I strongly suggest avoiding it :)

Array(+(832+"")[0]).join(".").split(".").map(function(_,i){ return (i+1)*Math.pow(10,(832+"").length-1);}); // 800,700,600,500,400,300,200,100

Array(+(3332+"")[0]).join(".").split(".").map(function(_,i){ return (i+1)*Math.pow(10,(3332+"").length-1);}); //1000,2000,3000
Sign up to request clarification or add additional context in comments.

6 Comments

Here is a one liner just to show possibility: new Array((8321/1000) | 0).join(".").split(".").map(function(_,i){ return (i+1)*1000});, but seriously, use a simple for loop, it's much more readable.
@BenjaminGruenbaum, yeah your function is great. But if getArrForNum(300) , 1000 is replaced with 100. So how can we determine whether it is thousand or hundred here. I want 1000 is a dynamic value.
This function produces wrong result for 20. getArrForNum(20) gives [10], where it should actually produce [20, 10].
@kadaj That's unspecified in the criteria of OP and can be easily modified to include the number 20 by changing the bound of the for loop (iterate until it's less than or equal, not just less than).
@BenjaminGruenbaum, I think it should be for(var i=1;i*mulOf10 <= num;i++){ here, so if we call getArrForNum(3000) then it will return 1000, 2000, 3000
|
2

The following will do what you require:

    function getArray(value) {

        var valueOrder = Math.floor( Math.log(value)  / Math.log(10) );
        var result = [];
        var step = Math.pow (10, valueOrder);
        var currentValue = step;

        while (currentValue < value) {
            result.push (currentValue);
            currentValue += step;
        }

        return result.reverse();    
    }

Demonstration here: http://jsfiddle.net/5zXc5/

    getArray(8939) => 8000,7000,6000,5000,4000,3000,2000,1000
    getArray(340) => 300,200,100

You haven't mentioned edge cases (e.g. 1000), but those can be handled easily enough by altering the < to a <= in the while, or adjusting the call to Math.floor.

Comments

2
function f(a) {
    var len = a.toString().length,
        m = Math.pow(10, len - 1),
        e = a.toString().charAt(0),
        num = e * m,
        i = 1
        out = [];
    for (i = 0; i < e; i++) {
      out.push(num - i * m);
    }
    return out;
}

f(8939); // [8000, 7000, 6000, 5000, 4000, 3000, 2000, 1000]  
f(340);  // [300, 200, 100]  
f(0);    // []
f(10);   // [10]
f(20);   // [20, 10]
f(70);   // [70, 60, 50, 40, 30, 20, 10]

Comments

1
function g(n){
    var p = Math.floor(Math.log(n)/Math.log(10)), // n.toString().length - 1 is cool (in others' answers).
        m = Math.pow(10,p),
        d = Math.floor(n/m),
        r = [];
    for(; d>0; d--){
        r.push(d*m);
    }
    return r;
}

g(8939); // [8000, 7000, 6000, 5000, 4000, 3000, 2000, 1000]

1 Comment

Just a fun fact, you can use Math.LN10 instead of Math.log(10)
1
function mine(num){
var result =[];
var count  = 0;
var i = num;
var f;
while(i!=0){
i = Math.floor(i/10);
count++;
}
f = Math.floor(num/Math.pow(10,count-1));
for(i =f;i>=1 && count>1;i--){
result.push(i*Math.pow(10,count-1));
}
return result;
}

mine(8939); // [8000, 7000, 6000, 5000, 4000, 3000, 2000, 1000]
mine(340); // [300, 200, 100]
mine(0); // []
mine(10); // [10]
mine(20); // [20, 10]
mine(70); // [70, 60, 50, 40, 30, 20, 10]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.