简体   繁体   中英

Why does repeat function not work the second time?

The challenge is to return an array that follows an arrow pattern when given a number. For example:

arrow(3) ➞ [">", ">>", ">>>", ">>", ">"]

I have almost completed it but it repeats the middle value in the array twice.

 function arrow(n) { var arr = []; var num = 1; while(num <= n) { arr.push(">".repeat(num)); num++; } while(num > 0) { arr.push(">".repeat(num - 1)); num--; } return arr; } console.log(arrow(3));

So then I changed it to this (for the second repeat I changed it to num - 2 but it says an error).

 function arrow(n) { var arr = []; var num = 1; while(num <= n) { arr.push(">".repeat(num)); num++; } while(num > 0) { arr.push(">".repeat(num - 2)); num--; } return arr; } console.log(arrow(3));

Can someone explain to me why this doesn't work?

The error with the first solution is that when num equals 3 , you increment it to 4 in the while loop. When the second while loop runs, num - 1 then equals 3.

In the second solution, num - 2 will equal -1 during the fourth iteration, which throws an error.

A for-loop may be easier to control here:

function arrow(n) {
    var arr = [];
    var num = 1;
    for (let i = 1; i <= n; i++) {
      arr.push(">".repeat(i))
    }
    for (let i = n - 1; i > 0; i--) {
      arr.push(">".repeat(i));
    }
    return arr;
}

The first one does not work because "num" is incremented a last time and thus equals "n + 1" when the code goes out from the while loop. So if "n" = 3, when the code executes the first "while(num > 0) {", num will equal 4. So 4 - 1 = 3 repetition of the arrow.

So, to fix it:

function arrow(n) {
    var arr = [];
    var num = 1;
    while(num <= n) {
        arr.push(">".repeat(num));
        num++;
    }
    num--; // add this line
    while(num > 0) {
        arr.push(">".repeat(num - 1));
        num--;
    }
    return arr;
}
console.log(arrow(3));

Your function does not work because you start the second loop when num is equal to n + 1 (which causes the middle value to be added twice) and do not end the loop until num is 0 (which causes an empty string to be appended to the result). For a simpler solution, you can use Array.from with a bit of arithmetic.

 function arrow(n) { return Array.from({length: n * 2 - 1}, (_,i)=>">".repeat(i < n? i + 1: n * 2 - 1 - i)); } console.log(arrow(3));

The issue with your second function:

when you were using the second while loop, the value of num was decreasing by 1 in each loop. when the loop value of num comes to 1, and and you tried to use arr.push(">".repeat(num - 2)); , then n-2 = -1 , but repeat(-1) is invalid function.


Solution:

I think in between two while loop, use num-- ; to decrease the value of num by 1. it will solve your problem.

 function arrow(n) { var arr = []; var num = 1; while(num <= n) { arr.push(">".repeat(num)); num++; } num --; while(num > 1) { arr.push(">".repeat(num - 1)); num--; } return arr; } console.log(arrow(3));

so when your loop get the last element number == 1 the repeat function (num-2) will not work for this challenger i simply put a (number--;) in the middle of the While loops i hope that work.

function arrow(n) {
    var arr = [];
    var num = 1;
    while(num <= n) {
    arr.push(">".repeat(num));
        num++;
    }
    num--; // take 1 out
    while(num > 1) {
        arr.push(">".repeat(num -1));

        num--;
    }
    return arr;
}

console.log(arrow(3));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM