Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

244
Views
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 4 years ago · Juan Pablo Isaza
2 answers
Answer question

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 4 years ago · Juan Pablo Isaza Report

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 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!