• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

163
Vistas
Print alternatively one element from the last and one from the first (JavaScript)

Print alternatively one element from the last and one from the first (JavaScript)

input 1, 3, 6, 3, 2, 8

output: 8, 1, 2, 3, 3, 6

about 3 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can use the below approach

var array = [];
var size = prompt('Enter Array Size'); //Maximum Array size

for(var i=0; i<size; i++) {
    
    //Taking Input from user
    array[i] = prompt('Enter Element ' + (i+1));
}

//Print the array in the console.
console.log("Array Input: "+array.join(','));

let output = [];
    let l = array.length - 1;
    for (let i = 0; i <= l; i++, l--) {
        if (i>=array.length/2) {
            console.log(array[i]);
            break;
        }
        output.push(array[l]);
        output.push(array[i]);
    }

console.log("Resultant Array: "+output.join(','));

about 3 years ago · Juan Pablo Isaza Denunciar

0

Anyways you can try the below logic.

const input = [1, 3, 6, 3, 2, 8];
let output = [];
for (var i = 0; i < input.length / 2; i++) {
  output.push(input[i]);
  output.push(input[input.length - (i + 1)]);
}

about 3 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda