PencilCoder

The I in I/O!

Goals:

  • Import data from an external file—from anywhere on the web—using Pencil Code's load function
  • Learn more about coding with asynchronous processes

Coding Snippets:

  • coding snippets
  • load accepts two arguments, a filename and a callback. load invokes the callback upon completion of the data import.
  • load passes data to the callback via its single argument (here, named data) as a single String. This example uses the String split method to separate that String into an array of String values, which it then converts to Numbers using parseInt.

Math and Computer Concepts:

  • The hypertext transfer protocol (HTTP) underlies nearly all interaction between a web page viewed in a computer's browser and the server(s) that provide the information that comprise that page.
  • Web browsers interact with external files using HTTP requests. Making HTTP requests with plain JavaScript or even with jQuery enhancements can be tricky. Pencil Code facilitates reading and writing to files on the web with its custom load and save functions.
  • HTTP requests are made asynchronously. This means that JavaScript starts the loading process and then moves on to process other code while waiting for the data transfer to complete. Use the await and defer functions as illustrated below to force your program to wait for the asynchronous load process to complete before moving on to subsequent code in your script:

Activities:

Save each of the following in a folder called IO.