Khan Academy CS – undocumented functions …

Useful functions and GUI algorithms for Khan Academy programs

After reading my introductory Khan Academy/CS page, you may find this page useful. I was planning to continue to add useful functions and tips as I find them, however, I’m a bit jaded with the KhanAcademy programming environment, so I’ll switch either to Java or if I want HTML5, to processing.js .


Tips:

  1. Print something to the console:
    println(“HELLO WEB:” + 12345);
  2. Print text to the screen:
    text(“Button is Clicked”, 154, 120);
  3. Set text font:
    // Possible fonts:
    // sans-serif, serif, monospace, fantasy, cursive
    var f = createFont(“monospace”, 20);
    textFont(f, 20);
    fill(255, 0, 0);
    text(“Hello!”, 100, 100);
  4. Make a rounded rectangle:
    rect(20, 20, 100, 20, 8);
  5. Make key:value pairs in an object (but the KH objects don’t seem to work like real Java objects)
    var o = {
    r: 'some value',
    t: 'some other value'
    };

    is functionally equivalent to
    var o = new Object();
    o.r = 'some value';
    o.t = 'some other value';
  6. Use the ternary operator: (how exactly does this work?)
    depth: parent ? parent.depth + 1 : 0,

§ Make random numbers — how do I do this?

§ Make a button:
I can do this, but I want to make a button object like in Java, and have myButton.setText(“hi”); and have a constructor which tells you where to place the button, as well as an onClick method.

§  What are the strange functions that are used in complex programs? They seem to be related to Javascript, but again, no documentation in Khan Academy.
? explosions.push(explosion);
? explosions.splice(i, 1);
? apply : function() {   //this is inside another function
? there is also a function called prototype().

updated December 2012