skip to Main Content

Iterate in a loop, increasing the received number by 2, up to a limit of 10 times. Save each new value in an array and return it. If at any point the sum value and the number of iterations coincide, the execution should be interrupted, and the string "Execution was interrupted" should be returned.

I have tried to make this function fulfill what they are asking me for, I have tried many ways but it only fulfills one of the two functions

This is the code

function breakStatement(num) {
  const nuevosValores = [];

  for (let i = 0; i < 10; i++) {
    num += 2
    nuevosValores.push(num);

    if (num === i) {
      console.log('se interrumpe la ejecucion del programa');
      break;
    }

  }
  return nuevosValores;
}

console.log(breakStatement(-4));

i tried to loops while and for, but cant find the solution for this enunciated.

i get from the console this.

when the parameter is

console.log(breakStatement(-4));

Expected: "interrupted execution"
    Received: [-2, 0, 2, 4, 6, 8, 10, 12, 14, 16]

and, when the parameter is

console.log(breakStatement(50));

Obtain a:

 Expected: [52, 54, 56, 58, 60, 62, 64, 66, 68]
    Received: undefined

2

Answers


  1. function breakStatement(num){
      const nuevosValores = [];
    
      for (let i = 0; i < 10; i++) {
        num += 2
        nuevosValores.push(num);
    
        if (num === i) {
          console.log('se interrumpe la ejecucion del programa');
          break;
        }
    
      }
    
      return nuevosValores;
    }
    
    console.log(breakStatement(50));

    seems like it’s doing whats expected, so probably your issue is somewhere else in the code or just remember to return the error message

    Login or Signup to reply.
  2. You need to return the error message.

    Beside this, the loop should run only nine times, according to the wanted result.

    function breakStatement(num) {
        const nuevosValores = [];
    
        for (let i = 1; i < 10; i++) {
            num += 2;
            if (num === i) return 'interrupted execution';
            nuevosValores.push(num);
        }
    
        return nuevosValores;
    }
    
    console.log(breakStatement(-4));
    console.log(breakStatement(50));
    .as-console-wrapper { max-height: 100% !important; top: 0; }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search