#100DaysOfCode Day 3

in #programming7 years ago

Untitled.jpg

Hey everyone, today has been another day with not much actual code work done but a lot more theory then usual. I know the whole idea of this challenge is to code every single day, but seeing as I am pressed for time, some theory will defiantly do.

Moving on, we have a somewhat hack day at work today, and I was paired up wit hone of our backend developers to get a crash course on Arrays. I had to make an array of 10 values which I wrote in the following way.

a = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]

While this code is perfectly valid and will work, it's not that easy to write or to change. Say if I wanted to make the array 20 values in length. It took me a whiletenbut my college helped me get the following to build a better array.

a = new Array(10).fill().map((v, i, a) => {return i+1})

So let's break down whats going on here. I'm defining a new Array with 10 values. I use fill to put something into the array and I use map to put something in there.
Map will take three arguments. Value, index and array. Buy using the function return i+1 I can increment through the array and get an output of the numbers 1 - 20.

Well, hope everyone enjoied this post.

And remember, keep coding 🤓

Coin Marketplace

STEEM 0.21
TRX 0.20
JST 0.034
BTC 90296.17
ETH 3085.37
USDT 1.00
SBD 2.95