2017-01-17 6 views
1

12345という数字を入力してこの数字を2桁に分割するプログラムを作成したいと思います数を配列に格納します。配列は次のようになります。[0] = 45 [1] = 23 [2] = 1。これは、数値の分割は、数値の最後の桁から始まり、最初の桁から始まらなければならないことを意味します。整数を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 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

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

+0

は、文字列 – dex412

+0

しかし、そのint型の端から起動してみてください?手伝って頂けますか? – Kenneth

+0

paresInt()メソッド – Seraf

答えて

3

異なる正規表現を使用して文字列を分割する奇数と偶数の文字列の長さ、Array#mapことがNumberを使用して、そしてArray#reverse配列:

function splitToNumbers(str) { 
 
    return str.match(str.length % 2 ? /^\d|\d{2}/g : /\d{2}/g).map(Number).reverse() 
 
} 
 
    
 
console.log(splitToNumbers('1234567')); 
 

 
console.log(splitToNumbers('123456'));

0

あなたのコードを改正

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(-2)); //Push last two characters into the splitCount array from line 1 
 
     if(count.length > 1) { 
 
      count = count.substr(0, count.length - 2); //Remove first last two characters from the count string 
 
     } else { 
 
      break; 
 
     } 
 
    }  
 
    console.log(splitCount) //Output our splitCount array 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

少し

+0

そのコードは私にとってはうまくいかないようですか? – Kenneth

+0

sry、間違った変数名 – Ayman

1

はこちら実現するスニペットタスクがありますか?この関数は値を配列myArrayに格納し、配列を<p>要素に出力します。番号は入力ボックスに入力されます。これは後で変更してください。

function myFunction() { 
 
    // Input field 
 
    var input = document.getElementById('num');1 
 
    // Length of the array storing the numbers 
 
    var myArraySize = Math.floor(input.value.length/2) + (input.value.length % 2); 
 
    // The array storing the numbers 
 
    var myArray = []; 
 
    
 
    for (var i = 0; i < myArraySize; i++) { 
 
    myArray[i] = input.value.slice(2*i,2*i+2); 
 
    } 
 
    // Output the array 
 
    document.getElementById('demo').innerHTML = myArray; 
 
}
<input id="num" type="text" onkeyup="myFunction()" /> 
 
<p id="demo">Result</p>

1

あなたは正規表現で文字列を分割し、配列を逆転できます。

この回答は重視されているanswerです。

var regex = /(?=(?:..)*$)/; 
 

 
console.log('12345'.split(regex).reverse()); 
 
console.log(''.split(regex).reverse());
.as-console-wrapper { max-height: 100% !important; top: 0; }

1

正規表現や高価な計算の必要はありません。あなたは単に次のようにします。

var n = 250847534, 
 
steps = ~~Math.log10(n)/2, 
 
    res = []; 
 
for(var i = 0; i <= steps; i++) { 
 
    res.push(Math.round(((n /= 100)%1)*100)); 
 
    n = Math.trunc(n); 
 
} 
 
console.log(res);

0

文字列を避け、数字上で直接動作します(Ori DoriまたはNina Scholzのような)正規表現を使用してワンライナー素敵からRedu's answerにあなたの問題にアプローチするにはいくつかの方法があります。

コード例とコメントのロジックに従うと、数値を文字列に変換し、それを逆方向にループして一度に2桁を抽出し、これを数値に変換します(unary plus演算子)、および結果の配列に出力する。この数:

function extract(num) { 
 
    var s = '0' + num, //convert num to string 
 
    res = [], 
 
     i; 
 
    for(i = s.length; i > 1; i -= 2) { 
 
    //loop from back, extract 2 digits at a time, 
 
    //output as number, 
 
    res.push(+s.slice(i - 2, i)); 
 
    } 
 
    return res; 
 
} 
 

 
//check that 12345 outputs [45, 23, 1] 
 
console.log(extract(12345)); 
 

 
//check that 123456 outputs [56, 34, 12] 
 
console.log(extract(123456));

関連する問題