Tag Archives: coding

TIL 27: javascript 8

Standard
Random numbers:
  • use Math.random() function to generate a random decimal number between 0 and 1 (0 can occur, but not 1)
  • random whole numbers can be generated with a combination of multiplication and Math.floor() to round the number down to the nearest whole number e.g. return Math.floor(Math.rand\rom() * 10);
  • to generate a whole number within a range, you can use something like: Math.floor(Math.random() * (max – min + 1)) + min
Regular expressions:
  • Regular expressions find patterns within strings
  • start and end a regular expression with ‘/’, with the pattern you want to find between, e.g. if you’re searching for the word ‘dog’, it’d be: /dog/
  • add g to the regex to make it global – ie will return all the matches in the string, not just the first one
  • add i to ignore case e.g. /dog/gi
  • there are many special selectors in regex, to select a type of value
  • \d is the digit selector – it would find one digit in a string e.g. /\d/g
  • after the selector (eg \d+) allows the matching of one or more
  • \s is the whitespace selector – this will find ” ” (space), \r (carriage return), \n (newline), \t (tab), and \f (linefeed)
  • invert a selector by using the uppercase letter e.g. \s selects whitespace, \S selects anything that is not whitespace
Advertisements

TIL 23: javascript 7

Standard

For loops:

  • loops allow the same bit of code to be run multiple times
  • For loops are the most common kind in javascript
  • the declaration format is: for ([initialisation]; [condition]; [final-expression])
  • the initialisation step only executes once, before the loop starts. Generally it is used to set up the loop variable
  • the condition statement is evaluated at the start of every loop, and the loop is only executed if this condition evaluates to true
  • the final-expression is executed at the end of each loop, before the next condition check, and usually increments or decrements the loop counter
  • e.g. for(var i = 0; i < 5; i+=2) { myArray.push(i)];} would result in [0, 2, 4]
  • For loops are often used to iterate through arrays, e.g. for(var i = 0; i < 5; i++) {console.log myArray[i]; }
  • nested for loops are used to loop through multi-dimensional arrays, e.g.

code

While loops:

  • as the name suggests, runs ‘while’ a specified condition is true
  • var i = 0; while (i < 5) { myArr.push(i); i++ }
  • note that the counter variable, i, has to be declared outside of the loop – while (var i < 5) would try to redeclare it on every loop
  • also note the need to increment the counter within the loop – if you forget this you’ll enter an infinite loop!

September Learning Challenge

Standard

Because hey – one challenge is never enough, right?! But since I’ve got Inktober, NaNoWriMo, and Christmas (cards to make…!) taking up the last three months of the year, it’s now or never with this one!

Anyway. Over on Habitica (and there’s a blog post I must unprocrastinate over ;)) I stumbled across a challenge based around the article, How to Learn Something New Every Day (And Actually Do Something With It).

stack-of-books-images-clipart-panda-free-clipart-images-NzH2Cp-clipart (1)First you pick your learning style – for me, reading is always the start – and topic. Ah, topic, singular? Well, that sounds dull! Or easy 😉 In case you haven’t noticed, this is a blog (sort of) about being a Scanner – someone with more interests than time. Doing the same thing for 30 days sounds like torture! Well, actually, it sounds like a really sensible plan, but let’s not let that stop me! No, me being me, I’ve got at least three topics (and quite frankly I could have had one for each day, I think!), and they might look a bit familiar from the links at the top:

  • Coding – always a priority for me, I think it’s high time I tackled javascript in a slightly more structured way.
  • Writing – Nano is coming (the new Stark motto), and there’s never a better time to learn some more about the craft.
  • Headology – aka ‘other stuff’. I’ve just started a stress management course (which is entirely appropriate for this madness right here!), and summarising those lessons should help them stick. I’ve also got a new book on habits, so a bit on that too.
  • Randomness – splitting the above into 2/week left me with a spare day. I could repeat one of my topics, but… while I can see it goes against the whole idea of a fixed curriculum, (a) I’m usually fully booked on a Friday, and (b) there’s always something that’s caught my eye to read later during the week, so let’s have a bit of a pot luck day!

Curriculum, you say? Why yes! Part of this is to lay out your plan of attack in quite a detailed manner before starting. Which has taken me HOURS (and almost put me off doing this whole thing quite frankly ;)) but here you can see the results of that toil:

learningChallenge

Phew! So far so good, but there’s another part to this challenge: you have to SHARE what you learn! Gulp!! This is another reason I wasn’t quite so keen to stick to one topic. Or rather, why I was reluctant to just pick coding – talking about coding is way harder than actually just, y’know, coding o_O

Some points for myself:

  • this is a *fun* challenge, for me by me. Don’t stress about it!
  • the curriculum is not carved in stone. See above, and move what needs moved
  • in fact, let’s have a weekly review and be willing to totally shake things up rather than give up!
  • the ‘Today I Learned…’ (TIL) posts should be short – I have to read/view/whatever AND write a post, AND continue with real life. I believe the original intention would have been a paragraph or so on e.g. Medium, but since this site is here (and a little less public…! o_O)

So. All going to plan (hah!) you can expect a slew of blog posts over the next 30 days. Well, I was almost certainly due a catch up 😉

Wish me luck!