fromecoderdojo

Inspiring kids to be creative and have fun with the internet

API Game programming Algorithms JavaScript HTML/CSS
7 April 2018

Algorithms in Javascript

by

Materials

Course #3

Algorithms introduction in Javascript.

Primitives

boolean
null
undefined
number
string

Complex

Object

JS is a dynamic language (loosely typed)

typeof(foo); 	 //returns the current type
var foo = 42;    // foo is now a number
var foo = 'bar'; // foo is now a string
var foo = true;  // foo is now a boolean

Coding time

The cinema problem. Can we order people in such way that EVERYONE sees the screen?

Hint: tall people in the back, small people in the front!

Brief animation to see how it works

Examples

bubbleSort([7,5,2,4,3,9]); //[2, 3, 4, 5, 7, 9]
bubbleSort([-9,7,5,4,3,1]); //[-9, 1, 3, 4, 5, 7]

Solution

More brain exercise

Let’s say now suddenly the cinema screen moves to the back of the room.

Tall people on the front, oh no!

Hint: What about changing the order of the already sorted list?

String challenge

You can think of a string as an array of characters right? Let’s try to do the following: reverse the letters

Examples

reverse('you are cool'); //"looc era uoy"

Solution

tags: