如何按照顺序(非随机)随机播放数字

时间:2018-08-09 07:05:51

标签: javascript function math

我需要构建一个函数,该函数返回一个给定的数字,将其改写后从数字的前面写一个数字,然后从后面写一个数字,然后从数字的前面写第3个数字,从后面写第4个数字,依此类推上。

示例:

const initialNumber = 123456应该返回 const finalNumber = 162534

const initialNumber2 = 104应该返回 const finalNumber2 = 140

此外,该数字应介于0到100.000.000之间。

该怎么做?我应该首先使用split()方法,然后使用for循环将数字转换为数组吗?

4 个答案:

答案 0 :(得分:3)

这可能是一种非常基本的算法。

Subscription.search(params[:query], explain: true, ...).response

答案 1 :(得分:3)

您可以通过将输入分为数组和dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') implementation 'com.android.support:appcompat-v7:28.0.0-beta01' implementation 'com.android.support.constraint:constraint-layout:1.1.2' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' implementation 'com.android.support:design:28.0.0-beta01' implementation 'com.android.support:cardview-v7:28.0.0-beta01' implementation 'com.google.android.gms:play-services-ads:15.0.1' } 来实现:

AdRequest adRequest = new AdRequest.Builder().build();
        interstitial = new InterstitialAd(Howtogeek.this);
        interstitial.setAdUnitId(getString(R.string.admob_interstitial_id));

        interstitial.loadAd(adRequest);
        interstitial.setAdListener(new AdListener() {
            public void onAdLoaded() {
                displayInterstitial();
            }
        });

        Howtogeek = (WebView)findViewById(R.id.Howtogeek);
        bar = (ProgressBar)findViewById(R.id.bar);
        Howtogeek.setWebViewClient(new Howtogeek.myWebclient());
        bar.setMax(100);
        Howtogeek.loadUrl("https://www.howtogeek.com/");

        WebSettings webSettings = Howtogeek.getSettings();
        webSettings.setJavaScriptEnabled(true);
        Howtogeek.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
        Howtogeek.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        Howtogeek.getSettings().setAppCacheEnabled(true);
        Howtogeek.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        webSettings.setDomStorageEnabled(true);
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        webSettings.setUseWideViewPort(true);
        webSettings.setSavePassword(true);
        webSettings.setSaveFormData(true);
        webSettings.setEnableSmoothTransition(true);
    }

    public void displayInterstitial() {
        if (interstitial.isLoaded()) {
            interstitial.show();
        }
    }

减少了一些代码:

reduce

答案 2 :(得分:2)

我希望将数字转换为字符串,然后在for循环中使用该字符串。

function shuffle(num) {
  var str = num.toString();
  var result = "";
  if(!isNaN(num) && num >= 0 && num <= 100000000) {
    for(var i = 0; i < str.length; i++) {
      if(i % 2 == 0) {
        result += str[Math.floor(i / 2)];
      } else {
        result += str[str.length - Math.floor(i / 2 + 1)];
      }
    }
  }
  return result;
}

console.log(shuffle(123456));  // 162534
console.log(shuffle(1234567)); // 1726354
console.log(shuffle(104));     // 140

答案 3 :(得分:2)

最好的方法是使用数组推入功能。

function shuffle(a) {
      var b = a.toString();
      var c = [];
      for(let i=0; i<b.length/2; i++) {
        c.push(b[i]);
        if(i==(Math.ceil(b.length/2)-1) && b.length%2==1) continue;
        c.push(b[b.length-i-1]);
      }
      return c.join("");
    }