JavaScript how to print array element from back side in increment loop print array element python,print array element bash,print array

let suppose I have an array var a = [1,2,3,4] and now I want to print this array from the back side like 4,3,2,1

I know this can we achieve by this

for(let i=a.length; i>= 0; i--) {
  console.log(a[i])  
  // output is  
  // 4
  // 3
  // 2
  // 1
}

but I want to print the output like 4,3,2,1 with the help of increment loop

for(let i=0; i < a.length; i++){
  console.log(a[i])
      // output should be 
      // 4
      // 3
      // 2
      // 1
}
Answer:1

You need just take the length and subtract the actual loop value an one for zero based approach.

var a = [1, 2, 3, 4];

for (let i = 0; i < a.length; i++) {
    console.log(a[a.length - 1 - i]);
}
Answer:2

Just while we are solving problems in the most backwards ways...

var arry = [1,2,3,4],
i = 1;
while (i <= arry.length) { console.log(arry[arry.length - i++]) }
Answer:3

I've written the following code that generates a select list. However, the part where it's supposed to check if this.id equals the same as the existing_code and put the selected value in doesnt seem ...

I've written the following code that generates a select list. However, the part where it's supposed to check if this.id equals the same as the existing_code and put the selected value in doesnt seem ...

  1. javascript foreach select options
  2. javascript foreach select

In VueJS, I have seen different ways of accessing parent properties from a component. Say I want to use the parent property items in my component. First way The component has a props value bound to ...

In VueJS, I have seen different ways of accessing parent properties from a component. Say I want to use the parent property items in my component. First way The component has a props value bound to ...

My application architecture is simple Front end using ionic-angularjs with backend nodejs interfacing with mongodb. My application will require ability to submit data related to reimbursement (say few ...

My application architecture is simple Front end using ionic-angularjs with backend nodejs interfacing with mongodb. My application will require ability to submit data related to reimbursement (say few ...

So I trying to Mock a date in my test, this is what I did : const mockDate = new Date('2018-01-01'); const backupDate = Date; beforeEach(() => { (global.Date as any) = jest.fn(() => ...

So I trying to Mock a date in my test, this is what I did : const mockDate = new Date('2018-01-01'); const backupDate = Date; beforeEach(() => { (global.Date as any) = jest.fn(() => ...

  1. mock date jest
  2. mock new date jest
  3. mock date.now jest
  4. mock date constructor jest
  5. mock current date jest
  6. mock date object jest
  7. mock global date jest
  8. moment mock date jest
  9. mock date gettime jest
  10. mock date function jest
  11. mock date javascript jest
  12. jest mock date timezone
  13. jest mock date toisostring
  14. jest mock date typescript
  15. jest mock date time
  16. jest mock date-fns
  17. jest mock date constructor typescript
  18. jest mock date class
  19. jest mock date tolocalestring
  20. jest mock date prototype