开发者

JAVASCRIPT will not update array outside loop

my problem is the following. Javascript seems to not update the values in arr_root in the loop 'for (let y = 0; y < 2; y++)'. The values of that remains the same,

  console.log("INSIDE: arr_root: " + arr_root);

even if:

  console.log("OUT: arr_root: " + arr_root);

Show's the updated values at the end of the loop.

The function should find the overlapping characters in order from root in word. So the resulting array 'overlaps' should be ['e', 'ice'] and not ['e', 'e'] whit input

  var word = "ice";
  var root = "device";

findWordPattern();

function findWordPattern() {
  var overlap_number = 0;
  var is_partial = false;
  var overlaping_charaters = [];
  var overlaps = [];
  var after_last_letter_found_pos = 0;
  var positions_to_delete_from_root = [];

  var word = "ice";
  var root = "device";

  //Split string into arrays
  var arr_word = word.split("");
  var arr_root = root.split("");
  console.log("ORIGINAL arr_word: " + arr_word);
  console.log("ORIGINAL arr_root: " + arr_root);

  for (let y = 0; y < 2; y++) {
    console.log("INSIDE: arr_word: " + arr_word);
    console.log("INSIDE: arr_root: " + arr_root);

    for (let i = 0; i < arr_root.length; i++) {
      for (let j = after_last_letter_found_pos; j < arr_word.length; j++) {
        if (arr_root[i] === arr_word[j]) {
          overlap_number++;
          overlaping_charaters.push(arr_root[i]);

          //changin j pos to look only after the first found letter, instead of re-itererating the wole array
          after_last_letter_found_pos = j + 1;
          positions_to_delete_from_root.push(i);
          break;
        }
      }
    }

    overlaps.push(overlaping_charaters);
    console.log("array overlaps: ");
    console.log(overlaps);

    //removing overlapping char from root
    for (let i = 0; i < positions_to_delete_from_root.length; i++) {
      console.log(
        "deleting " + arr_root[positions_to_delete_from_root[i]] + " from root"
      );
      arr_root.splice(positions_to_delete_from_root[i], 1);
      console.log("arr_root after splice: " + arr_root);
    }
    //resetting positions_to_delete_from_root
    positions_to_delete_from_root = [];
  }

  console.log("OUT: arr_word: " + arr_word);
  console.log("OUT: arr_root: " + arr_root);

  var longest = overlaps.sort(function(a, b) {
    return b.length - a.length;
  })[0];


  console.log({
    word: word,
    root: root,
    overlaping_charaters: longest,
    overlap_number: overlap_number,
    is_partial: is_partial
  });

  return {
    word1: word,
    root: root,
    overlaping_charaters: longest,
    overlap_number: overlap_number,
    is_partial: i开发者_如何学编程s_partial
  };
}

As I've already said the expred result in 'overlaps' should be ['e', 'ice'] and not ['e', 'e']. I've tried to create another variable in the for(y=0 ....) but it didn't change anything. Other variables are updated outside of the loop.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜