Monday, March 13, 2023

Debugging JavaScript, Part 3

 

General Debugging Tips

Top 7 Most Common Errors in Programming

https://www.learnacademy.org/blog/what-considered-common-programming-errors/

 

The 7 Most Common Types of Errors in Programming and How to Avoid Them

https://textexpander.com/blog/the-7-most-common-types-of-errors-in-programming-and-how-to-avoid-them

 

The Most Common Coding Errors

https://www.parkersoftware.com/blog/the-most-common-coding-errors/

 

JavaScript Debugging in Particular

JavaScript Debugging, W3Schools

https://www.w3schools.com/js/js_debugging.asp

 

16 JavaScript Debugging Tips You Probably Didn't Know (Advanced)

https://raygun.com/learn/javascript-debugging-tips

 

Strict Mode

W3 Schools

https://www.w3schools.com/js/js_strict.asp

 

MDN

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode

 

console.table

MDN

https://developer.mozilla.org/en-US/docs/Web/API/console/table

 

W3Schools

https://www.w3schools.com/jsref/met_console_table.asp

Debugging JavaScript, Part 2

JavaScript Lint Alternatives

JSLint is a very difficult tool to use. It is very picky. The author is very picky and opinionated about what he considers good JavaScript vs. bad JavaScript. He even wrote a book called JavaScript: The Good Parts. Anyway, his tool is not very configurable for beginner use even though it has a lot of options. I have found some lint alternatives that you may like to try.

1. ES Lint Online

ESLint is primarily a tool that you use as an application you install on your computer. However, there is a "playground" version where you can paste your code into an online form.

2. Validate JavaScript

ValidateJavaScript is an online validating (or linting) tool that will automatically find basic errors and help prevent potentially destructive bugs in JavaScript and JSX (React.js) code. Copy and paste or directly input your code into the editor, click the 'Find & Fix Errors' button, and the tool will parse your code and list all errors allowing you to fix them systematically.

 

3. JS Hint

Simply enter some JavaScript anywhere on the page. Your report will appear on the right side.

 
4. Prettier

This is just a code formatter, but it can do wonders for indenting your JavaScript properly, which makes it easier to find many different kinds of bugs.

 

5. ES Lint VSC Extension

You can get ESLint in the form of an extension you can add to Visual Studio Code. Unfortunately, this requires that you have installed ESLint as an application on your computer. This, in turn, requires npm (Node Package Manager) to do the install. So if you don't have npm, you'd have to install that first. This sounds like a good option, but I haven't had time to install everything myself.

https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint 

 

Debugging JavaScript, Part 1

Sometimes, perhaps often, we write code that has problems or just doesn't work. This module explores strategies for figuring out what's wrong and fixing it.

In general, we have two kinds of tools for exploring errors.

  • The console, which we can use to print messages at various points along the way in an effort to isolate the problems.
  • Debuggers, which help automate the task of find the errors. Debuggers fall into two categories:
    • The debugger built into the console, which may show you compile-time errors in your code.
    • Alternative debuggers, such as Firebug for Firefox, which may help you find errors in your code as it's running, known as run-time errors.

We may encounter two different kinds of errors as we're attempting to put a project together.

  • Compile-time errors are also known as syntax errors and they can be such mistakes as spelling errors, misplaced or missing parentheses, semicolons, commas, etc. These kinds of errors often prevent the program from running at all.
  • Run-time errors are not due to syntax mistakes but instead due to logic mistakes. So the program may run, but it may not do the right thing.

Compile-Time Errors

Compile-time errors include anything that is a violation of project JavaScript syntax. Common syntax errors include:

  • Spelling errors
    • Misspelled keywords, such as for, while, function, if, switch.
    • Misspelled library function and property names, such as getElementByID instead of getElementById, etc.
    • Mismatched variable names, such as colorswatches in one place, and colorSwatches in another place.
    • Note that like most programming langauges, JavaScript is case-sensitive, so mismatches in upper case vs. lower case letters can have a big impact.
  • Brackets errors
    • Extra or missing or mismatched brackets: parentheses ( ), square brackets [ ], curly brackets { }, angle brackets < > (usually in HTML, not JavaScript).
    • Using the wrong kind of brackets, particularly in
      • functions, which use both parentheses and curly brackets in different places,
      • arrays, which use square brackets,
      • objects, which use curly brackets, and
      • arrays of objects, which use both square brackets and curly brackets in different places.
  • Missing semicolons and commas.

Run Time Errors

Run time errors are hard to detect before running the program. There may not be any spelling errors that might be a warning that something is wrong. So when you run the program, it either

  • does the wrong thing, or
  • crashes, or
  • both.

Examples of common run-time errors include:

  • Forgetting to update the condition variable in a for or while loop, which can cause an infinite loop. (This can eventually cause a crash if the computer runs out of memory, or it may just lock up your computer until you quit and restart the browser.)
  • Trying to access an array index that is out of bounds (beyond the end of the array), which can cause a crash.
  • Trying to divide by zero, which can cause a crash. No one does this on purpose, but what if the user enters zero (0) and you try to use their input in a math calculation?
  • Not paying attention to operator precedence (PEMDAS), which can cause a calculation to return the wrong result, such as the wrong bank balance.
  • Wrong logic for if, &&, and ||, which can cause you, for example, to go outside if it is cold and rainy.

Saturday, September 17, 2022

JavaScript Crash Course Code


<!DOCTYPE html>
<html lang="en">
<head>
    <!--
    Mr. Brautigam
    JavaScript Crash Course
    -->
    <meta charset='utf-8' />
    <title>JavaScript Crash Course</title>
    <style>
    
        body { background-color: #438; 
            font-family: Courier, monospace;
        }
        h1 { text-align: center; color: lavender; 
            font-size: 1.75em;
        }
        div#console {
            background-color: lavender;
            padding: 0.5em;
            min-height: 10em;
            width: 75%;
            max-width: 1000px;
            margin: 1em auto;
            white-space: pre;
        }

    </style>
    <script>
        // do not change anything here
        function print (s) {
            document.getElementById("console").innerHTML += s;
        }
        
        function println (s) {
            if (typeof s !== 'undefined') {
                print (s)
            } 
            print ("\n");
        }
    </script>
</head>

<body>
    <h1>JavaScript Crash Course</h1>
    
    <div id='console'></div>

    <script type='text/javascript'>
    // your scripts will go here

        println ('This is the first line');

    </script>
</body>
</html>


Sunday, January 30, 2022

Advice for buying a Mac - January 2022 Edition

This is an update to my 2015 edition. Please read it first:

The quick version:

  • Your friend who works at Apple may be able to get you a discount.
  • Buy refurbished.
    • https://www.apple.com/
      • Scroll to bottom of page
      • Look for Refurbished and Clearance
    • Tech Restore, Small Dog, Mac Connection, and Mega Macs are all still in business.
    • Amazon? Just be sure to buy from Amazon or a reputable third party, not an individual.

How much memory?

  • The low end Apple models always come with 8 GB of RAM. This is never enough.
  • If you wish to run Xcode, get at least 16 GB of RAM.

How much disk?

  • The low end Apple models always come with 256 GB of SSD. This is usually enough for Xcode alone.
  • If you wish to subscribe to Adobe products or do audio processing, consider upgrading to 512 GB of SSD. This allows room for all the Adobe programs, which take up a lot of space.
  • If you wish do do video processing in the future or just make videos in general, consider upgrading to 1 TB of SSD. Video files can be very large.
  • Upgrading to 1 TB of internal SSD may cost $400 more than 256 GB. As an alternative, many choose to buy 1 TB of external spinning disk (around $50) or SSD (around $100). This can be effective if you expect to have a lot of video files. But it's better to keep large programs like Adobe on the internal drive.
  • The latest models come with a USB-C connector that may not connect to your USB-A devices. You may need to buy an inexpensive adapter (about $10).

Upgrades? On the Macs since around 2015, doing your own upgrades is rarely possible. Choose the most memory and disk you can afford at the time you buy it, and assume you won't be able to upgrade either RAM or SSD in the future.

Keyboard. Mac laptop keyboards from late 2015 until early 2018 (called "butterfly") are known to have problems. Don't buy a computer from before 2015; they are good, but not good enough for Xcode. Buy a computer from late 2018 or later and if it's a laptop, make sure to get a "scissor" keyboard.

Operating system and Xcode. 

  •  You can run Xcode 12.4 on "Catalina," vintage 2019. We have this in the classroom. You may need a computer made after 2014 or so to run this. You can talk to a device running up to iOS 14.4 with this setup.
  • You can run Xcode 12.5 or 13+ on "Big Sur," vintage 2020. You may need a computer made after 2015 or so. You can talk to a device running iOS 14.6 or later.
  • The latest Macs come with "Monterey," which came out in Fall 2021. You can talk to the latest versions of iPhones and iPads.

Comparison of models:

  • Mac Mini requires you supply your own monitor, keyboard, and mouse. Also requires adapter cables for all those. Suitable for intense work for 3–4 years.
  • Mac Book Air is suitable for intense work for 3–4 years. 
  • Mac Book Pro can last for 8–10 years. The more powerful processor will keep going longer.
  • The latest models (2021–2022) all have the new M1 chip, instead of the older Intel i5 or i7 chip.

Current prices for 16 GB of RAM:

Model256 GB512 GB256 EDUC512 EDUC
Mac Mini90011008301030
Mac Book Air1200140010801260
Mac Book Pro1500170013801580

At the top of each product page, I see a link that says Shop Education Store. I see lower prices when I click on that link. You can see those lower prices above. Save about $70 (7%) on Mac Mini. Save about $120 (10%) on Mac Books.


Thursday, June 27, 2019

Summer Cruise Reading

While on a cruise, it's much easier to make time to read, especially when you have three weeks! I brought six small books with us, of which I read four. I found five other books in the ship's library (and many more of interest to my wife). I bought one in a public library we visited on land in Homer, Alaska.


After Daybreak. The Liberation of Bergen-Belsen, 1945
Ben Shephard

From the ship's library. How and why did it took so long to get medical aid for the survivors of the first concentration camp liberated just before the end of World War II in Europe? 

Considers racism, battlefield logistics, medical training, and military bureaucracy. Frankly discusses the condition of the camps and their inhabitants; not for those with weak stomachs. Also discusses the long-term mental health issues faced by the survivors based on interviews taken decades later.



The Fred Factor
Mark Sanborn

Our school gives it to all the teachers. You can go above and beyond expectations with just a little effort. It's a quick read with many examples and anecdotes, but one wonders how many of the stories were invented to make a point.

Our school gives out "Fred awards" at the faculty meetings every month.



Holy Lands: One Place, Three Faiths
Thomas Cahill

The first book I found in the ship's library. It is mostly a pictorial book. It describes what we know of the histories of Abraham, Moses, David, Jesus, Mohammed, and others. Then it discusses the history of the region beginning with Zionist immigration in the late 19th century until the current day.

Reflections of Ireland
Photos by Bernard McCabe and Alain Le Garsmeur
Text by James Joyce

I found this book in the ship's library. It is a picture book with images accompanied by passages from the books of James Joyce. We intend to visit Ireland later this summer. I thought this book might show us a little of the Dublin mindset. Reading Joyce was a bit depressing. It seems his intent is to show how Dubliners act and speak, but they don't seem too happy. We'll soon find out if they are really unhappy or if that's just Joyce projecting his own world view onto everyone.



The Principles of Object-Oriented JavaScript
Nicholas C. Zakas

I brought this short book with us. It's basically JavaScript for those who are used to or prefer the C++ object model. It covers types, functions, objects, constructors, prototypes, inheritance, and object patterns. My own coding doesn't usually need to go that deep into objects, but I did learn a lot.



Messages from Earth. Nature and the human prospect in Alaska
Robert B. Weeden

I bought this book from the public library in Homer, Alaska. I thought I would learn a lot about the Alaska mindset. However, this book, by a professor of ecology retired from University of Alaska at Fairbanks, is written from the standpoint of an ardent environmentalist. So his thoughts aren't necessarily those of many Alaska inhabitants. 

I was put off by two things in this book. First, the writer's vocabulary is huge, and because he's a biologist, much of his vocabulary is unfamiliar to me. Sometimes it seems as if he's in a pissing contest to see how many big words he can use, and will you understand? This was definitely the most difficult book of all that I read, and at sea, I didn't have Google to look up the big words.

Second, the author uses charged language. For example, to describe how the government at Juneau dictates policy in faraway northern Alaska, and how the government in Washington dictates policy in faraway Alaska, Weeden uses the word "colonialism." To describe any business interest at all, Weeden uses the word "capitalism" in a pejorative way. This will not endear him to readers who don't buy unto his ideas 100%.



The History of the New Testament in Plain Language
Clayton Harrop

This book discusses transmission (how the manuscripts were written and copied) and canonicity (how it was decided which documents would go into the New Testament). Although I'm familiar with the topics already, I did learn a few things. I would recommend this book as an easy read for anyone interested in the topic.

If I were to teach a class on this topic (and that may happen soon), I would choose this as a textbook because it is short, accessible, and inexpensive.



Race Across Alaska
Libby Riddles with Tim Jones

I found this book in the ship's library and got it for my wife, who loved it. I didn't intend to at first, but I read it after she was done. Libby Riddles wrote the story of the journey and her preparation. Tim Jones wrote sections where he describes the race rules and history.

The race is a lonely event, one person and their dogs with occasional fleeting human interaction at the checkpoints. It is an excellent introduction to the race, its history and its challenges. 



Oaxaca Journal
Oliver Sacks

From the ship's library. Dr. Sacks delivers his actual journal from a ten-day trip to Oaxaca to study ferns with a group of amateur botanists. It's amazing that he could write this much in just the breaks during a busy trip that lasted little more than a week. 

As always, Dr. Sacks is delightful as he discusses Cortes and the conquistadors; tobacco and smoking; Columbus; the Inquisition; chiles; cacao and the history of chocolate; the amino acids in beans and corn; fern reproduction; crop rotation, nitrogen depletion, and synthetic fertilizers; Benito Juarez and the Mexican war of independence; Zapotec vs. Aztec culture; chemistry jokes; Flatland; castor beans, Castrol, and castor oil; police corruption; and the Catholic church. 

If you are interested in any of these topics, or even if not, this is a quick and insightful read.



Dr. Steve's Almanac of Christian Trivia
Steve Wilkens

Dr. Wilkens is a professor of theology at Azusa. This mad romp carries you through 2000 years of history in no particular order and with tongue in cheek. Sample topics include how to pronounce God's name, snake handling, the Gideons, Henry VIII, St. Patrick, the Serenity Prayer, the Amish, the origins of Sunday School, Michelangelo and the Sistine Chapel, the Salvation Army, Martin Luther and Katie, and when is Easter.

Every day on the ship, there was a meeting for "Friends of Bill W." In this book, I learned who those friends are. 



Wednesday, January 24, 2018

Poscietry

Little by little and day by day
The Universe fritters itself away
As gases mix, as starlight wanes,
Til randomness alone remains.

As rivers flow, as nations meet,
All order slowly turns to heat.
You can't escape the great decline–
Disorder is the world's design.

So who am I to buck the odds
And flout disorder-loving Gods?
It's for this reason, I confess,
I always leave my desk a mess.

~ Michael Cowan, JIR, 1982

Sunday, December 24, 2017

Christmas Pies

My apple and cherry pies. I definitely had no idea what I am doing. They look like they exploded. Let's hope they taste better than they look!


For the apple pie, I didn't have enough apple pie filling, so I just cut up another fresh apple and added it. The cherry pie also seemed to be short on filling but another whole can would have overflowed.


Wednesday, July 12, 2017

My Favorite Piano Music

YouTube playlist:
https://www.youtube.com/playlist?list=PLoqipQPAfFOp_E0sVVOKIN8dNlovvD54f
Total time about 43 minutes

1. At An Old Trysting Place
Edward MacDowell: Woodland Sketches, Op. 51, No. 3
Played by James Barbagallo
2. Couplet Sans Paroles
Emil Sauer
Played by Oleg Marshev
3. The Harmonious Blacksmith
George Frideric Handel: Keyboard Suite No. 5 in E major, HWV 430
Played by Balazs Szokolay
4. Once Upon a Time
Edward Grieg: Lyric Pieces, Book 10, Op. 71, No. 1
Played by Balazs Szokolay
5. Polka Italienne
Sergei Rachmaninov
Played by Balazs Szokolay
6. That Certain Feeling
George Gershwin
Played by George Gershwin on piano roll
7. The Ragtime Nightingale
Joseph Lamb
Played by Joshua Rifkin
8. Kickin' The Clouds Away
George Gershwin
Played by George Gershwin on piano roll
9. Tea For Two
Youmans and Caesar
Arranged and played by Claude Bolling
10. Norwegian Dance Op. 35, No. 2
Edward Grieg
Played by Cyprien Katsaris
11. Death Ray Boogie
Claude Bolling
Played by Claude Bolling
12. At the Cradle
Edward Grieg: Lyric Pieces, Op. 68, No. 5
Played by Balazs Szokolay
13. In Autumn
Edward MacDowell: Woodland Sketches, Op. 51, No. 4
Played by James Barbagallo


Thursday, July 6, 2017

Gluten Free Falafel

Oren's Hummus gluten free falafel sandwich.

We find so many cool things using the Find Me Gluten Free app on our phones.