Tag Archives: javascript

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

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!

TIL 20: javascript 6

Standard

Objects (continued):

  • Objects can be thought of as key/value storage, like a dictionary
  • Objects can be used to store data as lookup tables

code

  • check if an object property exists with .hasOwnProperty(propertyName); which returns true or false
  • a javascript object is one way to handle flexible data, allowing for artbitrary combinations of different data types
  • complex data structures might be stored as objects inside an array
  • objects properties are key-value pairs, e.g. “artist”: “Beatles” is a property with key “artist” and value “Beatles”
  • objects can be nested e.g.

code

  • access the sub-properties of nested objects by chaining the dot or bracket notation, as shown above
  • nested arrays are accessed in a similar manner, e.g. arr1[1].arr2[0];
  • JSON (JavaScript Object Notation) is a related data interchange format used to store data.

TIL 13: javascript 4

Standard

Pressing on with freeCodeCamp! Start at the beginning here or previous installment.

Booleans:

  • true or false
  • nb NOT “true” and “false”

Conditional logic:

code

  • ‘myCondition’ above could also be a comparison operate that equates to true or false. E.g. if(myVal == 10); where ‘==’ is the equality operator (nb do not use assignment ‘=’ by mistake!) or if(thatVal > 9);
  • note that javascript will attempt to convert data types. Thus, 1 == ‘1’ and “3” == 3
  • to get past this conversion, use strict equality to test for value and type equality e.g. 3 === 3 is true, but 3 === ‘3’ is false
  • the inequality operator is ‘!=’, and likewise ‘!==’ for strict inequality
  • logical AND operator ‘&&’ allows two conditions to be tested, with a ‘true’ result requiring both to be met
  • logical OR operator ‘||’ returns true if either condition is met
  • else statements allow for alternate code paths to be triggered e.g. if x do y; else do z;
  • else if allows the chaining of multiple if statements e.g. if a do x, else if b do y, else do z;

Switch statements:

code

  • tests a value and can have many case statements defining various outcomes
  • can be easier to write than chained if/else if statements
  • cases are tested with strict (===) equality
  • a default option can/should be provided for when none of the cases are true
  • multiple cases can evaluate to the same outcome by omitting a break statement, e.g. case 1: case 2: case 3: result = “1, 2 or 3”; break;

Returning Boolean Values (e.g. from Functions):

  • instead of if (a === b) {return true }; else {return false}; you can simply write return a === b; since this returns true or false automatically

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!