Splitting the random string into two steps And do the math on the final result (js -jquery)

huangapple go评论60阅读模式
英文:

Splitting the random string into two steps And do the math on the final result (js -jquery)

问题

function stringSplitter(myString, chunkSize) {
    var splitString = [];
    for (var i = 0; i < myString.length; i = i + chunkSize) {
        splitString.push(myString.slice(i, i + chunkSize));
    }
    return splitString;
}

function splitAndProcess(inputString) {
    var step1 = stringSplitter(inputString, 4);
    var output = [];

    for (var i = 0; i < step1.length; i++) {
        var step2 = stringSplitter(step1[i], 1);
        // Perform your math actions here using step2
        // For example: var result = step2[0] + step2[1] + step2[2] / 100;
        // Push the result to the output array if needed
        output.push(result);
    }

    return output;
}

var randnumber = 'va2b1881ff21GSD1SSSS18aav1v112H1tkdpkmmnnwqkkq118188493';
var resultArray = splitAndProcess(randnumber);

Please note that the actual math actions you want to perform are not specified in your request, so you'll need to replace the placeholder code inside the splitAndProcess function with your specific calculations.

英文:

I want to split random strings generated by my code,

But here I just use static string for example|

i can do it manual ,but its boring to this in the long time and large scale code

i want function , to do this easily

here is my codes :

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function stringSplitter(myString, chunkSize) {
  var splitString = [];
  for (var i = 0; i &lt; myString.length; i = i + chunkSize) {
    splitString.push(myString.slice(i, i + chunkSize));
  }
  return splitString;
}
var randnumber = &#39;va2b1881ff21GSD1SSSS18aav1v112H1tkdpkmmnnwqkkq118188493&#39;;

var get4 = stringSplitter(randnumber, 4);

var str1 = $(&quot;#get&quot;).html(get4);
var str2 = $(&quot;#get2&quot;).html(get4[0]);
var str3 = $(&quot;#get3&quot;).html(get4[1]);
var str4 = $(&quot;#get4&quot;).html(get4[2]);

//.................
var get2of4_1 = stringSplitter(get4[0], 1);
var str5 = $(&quot;#td1&quot;).html(get2of4_1[0]);
var str6 = $(&quot;#td2&quot;).html(get2of4_1[1]);
var str7 = $(&quot;#td3&quot;).html(get2of4_1[2]);
var str8 = $(&quot;#td4&quot;).html(get2of4_1[3]);

var get2of4_2 = stringSplitter(get4[1], 1);
var str9 = $(&quot;#td5&quot;).html(get2of4_2[0]);
var str10 = $(&quot;#td6&quot;).html(get2of4_2[1]);
var str11 = $(&quot;#td7&quot;).html(get2of4_2[2]);
var str12 = $(&quot;#td8&quot;).html(get2of4_2[3]);

var get2of4_3 = stringSplitter(get4[2], 1);
var str13 = $(&quot;#td9&quot;).html(get2of4_3[0]);
var str14 = $(&quot;#td10&quot;).html(get2of4_3[1]);
var str15 = $(&quot;#td11&quot;).html(get2of4_3[2]);
var str16 = $(&quot;#td12&quot;).html(get2of4_3[3]);

//for example :

var sum = str5 + str6 + str7 / 100

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;

&lt;head&gt;
  &lt;meta charset=&quot;UTF-8&quot;&gt;
  &lt;title&gt;&lt;/title&gt;
  &lt;script src=&quot;../!Needs/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;

  &lt;hr&gt;
  &lt;span id=&quot;get&quot;&gt;
    ...........
    &lt;/span&gt;
  &lt;hr&gt;
  &lt;br&gt;
  &lt;span id=&quot;get2&quot;&gt;&lt;/span&gt;
  &lt;br&gt;
  &lt;span id=&quot;get3&quot;&gt;&lt;/span&gt;
  &lt;br&gt;
  &lt;span id=&quot;get4&quot;&gt;&lt;/span&gt;
  &lt;br&gt;
  &lt;span id=&quot;get5&quot;&gt;&lt;/span&gt;
  &lt;br&gt;&lt;br&gt;
  &lt;table border=&quot;1&quot;&gt;
    &lt;tr&gt;
      &lt;td id=&quot;td1&quot;&gt;&lt;/td&gt;
      &lt;td id=&quot;td2&quot;&gt;&lt;/td&gt;
      &lt;td id=&quot;td3&quot;&gt;&lt;/td&gt;
      &lt;td id=&quot;td4&quot;&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td id=&quot;td5&quot;&gt;&lt;/td&gt;
      &lt;td id=&quot;td6&quot;&gt;&lt;/td&gt;
      &lt;td id=&quot;td7&quot;&gt;&lt;/td&gt;
      &lt;td id=&quot;td8&quot;&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td id=&quot;td9&quot;&gt;&lt;/td&gt;
      &lt;td id=&quot;td10&quot;&gt;&lt;/td&gt;
      &lt;td id=&quot;td11&quot;&gt;&lt;/td&gt;
      &lt;td id=&quot;td12&quot;&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/table&gt;
&lt;/body&gt;

&lt;/html&gt;

<!-- end snippet -->

i split my string twice .
first i got 4 characters , then i splite 4 characters to 1

for Example : abcdefghijklmnopq 4 =&gt; (abcd) 1 =&gt; (a)(b)(c)(d)

then i want to do some math actions ,

but my problem is . is there and function or simple way to do this, faster and easily ?

i dont want to write several times

and i dont know how to use loop or functions , to make this :

1 =&gt; Split random strings into 4 characters ,
2 =&gt; Split again that 4 characters into 1 characters
3 =&gt; doing some action at final step

i need a function or loop to use,
thank you Splitting the random string into two steps And do the math on the final result (js -jquery)

答案1

得分: 0

以下是翻译好的部分:

这段代码使用嵌套的 ForEach 循环来迭代每个数组。

英文:

Consider the following.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(function() {
  function stringSplitter(myString, chunkSize) {
    var splitString = [];
    for (var i = 0; i &lt; myString.length; i += chunkSize) {
      splitString.push(myString.slice(i, i + chunkSize));
    }
    return splitString;
  }
  var randnumber = &#39;va2b1881ff21GSD1SSSS18aav1v112H1tkdpkmmnnwqkkq118188493&#39;;

  var get4 = stringSplitter(randnumber, 4);
  $(&quot;#get&quot;).html(get4);

  $(&quot;#output-1&quot;).html(get4.join(&quot;&lt;br /&gt;&quot;));
  
  var j = 0;
  $.each(get4, function(i, el) {
    var row = $(&quot;&lt;tr&gt;&quot;).appendTo(&quot;#output-2&quot;);
    var cells = stringSplitter(el, 1);
    $.each(cells, function(c, val) {
      $(&quot;&lt;td&gt;&quot;, {
        id: &quot;str-&quot; + (++j)
      }).html(val).appendTo(row);
    });
  });
  
  var sum = parseInt($(&quot;#str-5&quot;).text()) + parseInt($(&quot;#str-6&quot;).text()) + parseInt($(&quot;#str-7&quot;).text()) / 100;
  console.log(sum);
});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;

&lt;head&gt;
  &lt;meta charset=&quot;UTF-8&quot;&gt;
  &lt;title&gt;&lt;/title&gt;
  &lt;script src=&quot;../!Needs/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;

  &lt;hr&gt;
  &lt;span id=&quot;get&quot;&gt;
    ...........
    &lt;/span&gt;
  &lt;hr&gt;
  &lt;div id=&quot;output-1&quot;&gt;&lt;/div&gt;
  &lt;table id=&quot;output-2&quot; border=&quot;1&quot;&gt;
  &lt;/table&gt;
&lt;/body&gt;

&lt;/html&gt;

<!-- end snippet -->

This uses nested ForEach loops to iterate each of the arrays.

huangapple
  • 本文由 发表于 2023年2月14日 00:04:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75438365.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定