Home > Mobile >  Countdown 3rd array element and print sorted even numbers
Countdown 3rd array element and print sorted even numbers

Time:08-17

I am trying to solve this task I have and I do not seem to know what is the issue with it exactly. My task requires the following; 1- countdown from a given number by 3 2- only print the even numbers 4- sorted

what I did so far;

function cd (num){
  let arrayCount = [];
  let arrayEven = [];
        for(let i = 0; i <= num; i  ) {
              arrayCount.push(num-i);
        }
        //return arrayCount;
       for (let j = 0; j <= arrayCount.length; j =3) {
           if (arrayCount[j] % 2 == 0) {
            arrayEven.push(arrayCount[j]);
           }
       }
       arrayEven.sort(function(a,b){return a - b;});
       return arrayEven;
    
}
console.log(cd(10)); // expected output [4]
console.log(cd(23)); // expected output [2,8,14,20]
console.log(cd(103)); // expected output [4,10,16,22,28,34,40,46,52,58,64,70,76,82,88,94,100]
console.log(cd(15)); //expected output [6,12]

My issue is with input 15 and 10 and I am not sure what I am doing work. Any help would be appreciated.

CodePudding user response:

When I run your code, I see that it fails test 1 and test 4:

console.log(cd(10)); // expected output [4]    // your output [4,10]
console.log(cd(15)); //expected output [6,12]  // your output [0,6,12]

So, there are three possibilities:

  • your code is wrong
  • a test case is wrong
  • both your code and test are wrong

As this is a question, you should be able to assume that the test cases will always be correct (though this is not always true). However, in real life, that is not the case.

You should read the question carefully to make sure you understand exactly what the question is asking for. What is interesting about the two cases that fail is that they are edge cases. In test case 1, you return a 10 which is also the starting number. In test case 4, you return a 0 which is not an even number.

It is obvious from test case 4 that something is wrong with your algorithm and you need to fix that.

However, test case 1 is more interesting. From your description of the question, which I understand is not the full text of the question, it is not clear whether you should start at 10 and count down by 3, ignoring the 10. Only a very careful reading of the question will clarify this.

In your answer, you may consider making an assumption that the requirement is to start counting down from a number, ignoring that first number. For example:

My understanding of the question is that we start counting down from a number and we do not include that number in the results.

Or, of course, you could ask somebody for clarification before submitting your solution.

As for your algorithm, you don't need to use two arrays and I don't think you need to use two for loops.

CodePudding user response:

So, I figured out what was missing. The thing is that the loop would always print the first element and THEN apply the conditions you want on the rest of the elemnts, and that is why in input [10] the number 10 was printed. In short, I just needed to let my loop start from i = num-3. Thank you for you comments as they helped me a lot. Although the solution was right there, still I wanted to share the updated part, so here you go:

for (let i = num-3; i > 0; i-=3) {
        if (i % 2 == 0) 
            array.push(i);
      } let result = array.sort((a, b) => a - b);
  • Related