2017-01-17 7 views
0

数字を入力するプログラムを作成したいと思います。たとえば、12345を作成し、この数字を2桁の数字に分割して配列に格納します。配列は次のようになります。[0] = 45 [1] = 23 [2] = 1。これは、数値の分割は、数値の最後の桁から始まり、最初の桁から始まらなければならないことを意味します。Array数字の最後の2桁を削除します

これは私が今まで持っているものです。

var splitCount = []; // This is the array in which we store our split numbers 
 
//Getting api results via jQuery's GET request 
 
$.get("https://www.googleapis.com/youtube/v3/channels?part=statistics&id=UCJwchuXd_UWNxW-Z1Cg-liw&key=AIzaSyDUzfsMaYjn7dnGXy9ZEtQB_CuHyii4poc", function(result) { 
 
    //result is our api answer and contains the recieved data 
 
    //now we put the subscriber count into another variable (count); this is just for clarity 
 
    count = result.items[0].statistics.subscriberCount; 
 
    //While the subscriber count still has characters 
 
    while (count.length) { 
 
     splitCount.push(count.substr(0, 2)); //Push first two characters into the splitCount array from line 1 
 
     count = count.substr(2); //Remove first two characters from the count string 
 
    }  
 
    console.log(splitCount) //Output our splitCount array 
 
});

が、これに伴う問題は、例えば、5桁の数字がある場合ということである:12345は、最後の数字が配列になりますそれ自体では次のようになります:[0] = 12 [1] = 34 [2] = 5しかし、私は最後の配列を2桁にする必要があり、最初は1桁の数字でなければなりません。[0] = 1 [ 1] = 23 [2] = 45

+0

あなたは何を試してみましたか?すでに行ったことを示すコードはありますか? – birryree

+0

入力は常に整数であると仮定していますか? – virtuexru

+0

はい、私はこのコードを持っています(上に追加) – Kenneth

答えて

0

非常に粗いですが、これは仮定して動作するはずです文字列は常に数字です:

input = "12345" 

def chop_it_up(input) 
    o = [] 

    while input.length > 0 
    if input.length <= 2 
     o << input 
    else 
     o << input[-2..input.length] 
    end 
    input = input[0..-3] 
    chop_it_up(input) 
    end 

    return o 
end 
+0

私はJSでそれが必要だと言って忘れました – Kenneth

+0

Rubyで書いたことをJSに変換するのは難しくありません。 – virtuexru

+0

それはおそらくisntですが、私はコーディングにはまったく新しく、わずか15であり、Rubyについての手掛かりはなく、現在JSを学ぼうとしています。あなたが私にそれを助けることができれば、本当に感謝します – Kenneth

0

私はおそらくこのようSTHん:

int[] fun(int x){ 
    int xtmp = x; 
    int i = 0; 
    int len = String.valueOf(x).length(); 
    // this is a function for java, but you can probably find 
    //an equivalent in whatever language you use 
    int tab[(len+1)/2]; 
    while(xtmp > 1){ 
     tab[i] = xtmp%100; 
     xtmp = int(xtmp/100); // here you take the integer part of your xtmp 
     ++i; 
    } 
    return tab; 
} 
関連する問題