Looping Through An Array and Dividing Each Value By 100

后端 未结 5 1281
Happy的楠姐
Happy的楠姐 2021-01-27 03:44

Hey this is my first question and I\'m just finding my way with JS. I have an array which is set, I want to loop through it and divide each value by 100, then print the output t

5条回答
  •  难免孤独
    2021-01-27 04:41

    UPDATED to save the modified values in-place

    There's not much reason to log() each value separately. You could streamline the code with something like this, with pretty ES6 arrow functions and ES5 array methods:

    var example = [5, 10, 15, 20]
    
    console.log(example = example.map(x => x/100))
    

    Note: arrow functions such as x => x/100 currently work in a very small subset of Web browsers. If you want to publish a web app or page right now, you should use a standard anonymous function: function (x) { return x/100 }.

提交回复
热议问题