'tag.htm'; break; case 'flag': $pre .= $default_pre .= 'flag.htm'; break; case 'my': $pre .= $default_pre .= 'my.htm'; break; case 'my_password': $pre .= $default_pre .= 'my_password.htm'; break; case 'my_bind': $pre .= $default_pre .= 'my_bind.htm'; break; case 'my_avatar': $pre .= $default_pre .= 'my_avatar.htm'; break; case 'home_article': $pre .= $default_pre .= 'home_article.htm'; break; case 'home_comment': $pre .= $default_pre .= 'home_comment.htm'; break; case 'user': $pre .= $default_pre .= 'user.htm'; break; case 'user_login': $pre .= $default_pre .= 'user_login.htm'; break; case 'user_create': $pre .= $default_pre .= 'user_create.htm'; break; case 'user_resetpw': $pre .= $default_pre .= 'user_resetpw.htm'; break; case 'user_resetpw_complete': $pre .= $default_pre .= 'user_resetpw_complete.htm'; break; case 'user_comment': $pre .= $default_pre .= 'user_comment.htm'; break; case 'single_page': $pre .= $default_pre .= 'single_page.htm'; break; case 'search': $pre .= $default_pre .= 'search.htm'; break; case 'operate_sticky': $pre .= $default_pre .= 'operate_sticky.htm'; break; case 'operate_close': $pre .= $default_pre .= 'operate_close.htm'; break; case 'operate_delete': $pre .= $default_pre .= 'operate_delete.htm'; break; case 'operate_move': $pre .= $default_pre .= 'operate_move.htm'; break; case '404': $pre .= $default_pre .= '404.htm'; break; case 'read_404': $pre .= $default_pre .= 'read_404.htm'; break; case 'list_404': $pre .= $default_pre .= 'list_404.htm'; break; default: $pre .= $default_pre .= theme_mode_pre(); break; } if ($config['theme']) { $conffile = APP_PATH . 'view/template/' . $config['theme'] . '/conf.json'; $json = is_file($conffile) ? xn_json_decode(file_get_contents($conffile)) : array(); } !empty($json['installed']) and $path_file = APP_PATH . 'view/template/' . $config['theme'] . '/htm/' . ($id ? $id . '_' : '') . $pre; (empty($path_file) || !is_file($path_file)) and $path_file = APP_PATH . 'view/template/' . $config['theme'] . '/htm/' . $pre; if (!empty($config['theme_child']) && is_array($config['theme_child'])) { foreach ($config['theme_child'] as $theme) { if (empty($theme) || is_array($theme)) continue; $path_file = APP_PATH . 'view/template/' . $theme . '/htm/' . ($id ? $id . '_' : '') . $pre; !is_file($path_file) and $path_file = APP_PATH . 'view/template/' . $theme . '/htm/' . $pre; } } !is_file($path_file) and $path_file = APP_PATH . ($dir ? 'plugin/' . $dir . '/view/htm/' : 'view/htm/') . $default_pre; return $path_file; } function theme_mode_pre($type = 0) { global $config; $mode = $config['setting']['website_mode']; $pre = ''; if (1 == $mode) { $pre .= 2 == $type ? 'portal_category.htm' : 'portal.htm'; } elseif (2 == $mode) { $pre .= 2 == $type ? 'flat_category.htm' : 'flat.htm'; } else { $pre .= 2 == $type ? 'index_category.htm' : 'index.htm'; } return $pre; } ?>javascript - alter the direction of loop dynamically - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - alter the direction of loop dynamically - Stack Overflow

programmeradmin0浏览0评论

I am trying to e up with an algorithm for an "approaching" behavior between two integers. Basically, given two integers, a and b, i want a to "approach" b, even if b is less than a. The way i think this should look is a swapping of the loop incrementer function:

for (var i = a; approachCond(i, a, b); approachDir(i,a, b)) {
   // some fn(a, b);
}

where

approachCond(i, a, b) {
 return a < b ? i < b :  i > b;
}

and

approachDir(i, a, b) {
 return a < b ? i++ : i--
}

However, when i try doing this the browser freezes (Chrome). Does anyone know how to dynamically alter the direction of a loop?

I am trying to e up with an algorithm for an "approaching" behavior between two integers. Basically, given two integers, a and b, i want a to "approach" b, even if b is less than a. The way i think this should look is a swapping of the loop incrementer function:

for (var i = a; approachCond(i, a, b); approachDir(i,a, b)) {
   // some fn(a, b);
}

where

approachCond(i, a, b) {
 return a < b ? i < b :  i > b;
}

and

approachDir(i, a, b) {
 return a < b ? i++ : i--
}

However, when i try doing this the browser freezes (Chrome). Does anyone know how to dynamically alter the direction of a loop?

Share Improve this question edited Jul 29, 2015 at 20:13 dopatraman asked Jul 29, 2015 at 20:11 dopatramandopatraman 13.9k29 gold badges97 silver badges163 bronze badges 2
  • have an if else with a different for loop in each one – depperm Commented Jul 29, 2015 at 20:17
  • Thats what im doing now, but i want to avoid that if a more elegant solution is possible. – dopatraman Commented Jul 29, 2015 at 20:18
Add a ment  | 

6 Answers 6

Reset to default 3

I think it's a little clearer to read if you just use a while loop:

'use strict';
let a = 12, b = 6;

let i = a;

while (i !== b) {
  console.log(i);
  i += a < b ? 1 : -1;
}

I even left the cute ternary since people seem so opposed to if-statements these days.

Your browser freezes because you're not altering the correct i. You're only manipulating the i that is in the approachDir function. If you return it & set the for scope i to the new value, it will work.

Try:

for (var i = a; approachCond(i, a, b); i = approachDir(i,a, b)) {
   // some fn(a, b);
}

approachDir(i, a, b) {
    return a < b ? i + 1 : i - 1
}

It seems as though you are overplicating something that is not that hard. You can just set the step to positive or negative. e.g.

var a = 20;
var b = 5;

for (var step = a > b ? -1 : +1; a != b; a += step)
{
     console.log(a);
}

The problem is in approachDir. i++ and i-- are post-increment and post-decrement. That means they update the variable after they return its original value. So the function is returning the original value, not the updated one. To update the variable before returning, you should use ++i or --i.

But you don't need to use an increment operator at all, since the local variable is going away immediately. Just return the new value.

function approachDir(i, a, b) {
    return a < b ? i + 1 : i - 1;
}

You also need to reassign the variable in the loop:

for (var i = a; approachCond(i, a, b); i = approachDir(i, a, b)) {
    ...
}

The way you wrote your code, you assumed that variables are passed by reference, not by value, so that the increment in the function would modify the caller's variable.

One option is to change approachDir to return a positive or negative value based on if a>b. The middle statement can include an || and work either way.

function approachDir(a, b){
  return a>b?-1:1;
}
var a=3;
var b=1;
for (var i = a; i<b||i>b; i+=approachDir(a, b)) {
   console.log(i);
}
a=1;
b=3;
for (var i = a; i<b||i>b; i+=approachDir(a, b)) {
   console.log(i);
}

I made a slight change to @mathletics answer:

  function loopDynamic(from, to) {
    const direction = from < to ? 1 : -1;
    let i = from

    while (i!== to) {
      // do whatever with i

      i += direction;
    }
  }
发布评论

评论列表(0)

  1. 暂无评论