I’m in a reading rut. I need a book to read but I don’t know what to read. Should I look to Goodreads for their top list? Should I look to TikTok with their booktok choices? Reddit? All of these? That will take some time and I usually don’t have a lot of time. But I like to read. I like stories. And currently the only story I have been following is Final Fantasy xvi. Yes, I said 16. The story is pretty good. A typical medieval fantasy setting but with humans having the powers of godlike beings dictating how battles go. Mothercrystals sucking aether out of the land. Stuff like that.
Geez, Tommy doesn’t have a lunch for tomorrow and I’m normally in charge of his lunch in this house. Hmmm
In JavaScript, I’m a little stumped. We are doing data structures now and now I’m not sure what I’m doing. Not that I knew much of what I was doing before.
My journal should be a bunch of bullet points cause that is basically what I’m writing today.
I really need to clean my desk. I have mail that needs to be sorted out and/or thrown away or shredded. And I have cords everywhere. I just want it to look pretty.
I just noticed that my computer has updates and wants to restart. I guess I can let it do that.
JavaScript notes…
————————————————-
The console.log() and typeof methods are the two primary ways to check intermediate values and types of program output. Now it’s time to get into the common forms that bugs take. One syntax-level issue that fast typers can commiserate with is the humble spelling error.
Transposed, missing, or mis-capitalized characters in a variable or function name will have the browser looking for an object that doesn’t exist – and complain in the form of a reference error. JavaScript variable and function names are case-sensitive.
Another syntax error to be aware of is that all opening parentheses, brackets, curly braces, and quotes have a closing pair. Forgetting a piece tends to happen when you’re editing existing code and inserting items with one of the pair types. Also, take care when nesting code blocks into others, such as adding a callback function as an argument to a method.
One way to avoid this mistake is as soon as the opening character is typed, immediately include the closing match, then move the cursor back between them and continue coding. Fortunately, most modern code editors generate the second half of the pair automatically.
JavaScript allows the use of both single (‘) and double (“) quotes to declare a string. Deciding which one to use generally comes down to personal preference, with some exceptions.
Having two choices is great when a string has contractions or another piece of text that’s in quotes. Just be careful that you don’t close the string too early, which causes a syntax error.
Here are some examples of mixing quotes:
const grouchoContraction = "I've had a perfectly wonderful evening, but this wasn't it."; const quoteInString = "Groucho Marx once said 'Quote me as saying I was mis-quoted.'"; const uhOhGroucho = 'I've had a perfectly wonderful evening, but this wasn't it.';
The first two are correct, but the third is incorrect.
Of course, it is okay to use only one style of quotes. You can escape the quotes inside the string by using the backslash (\) escape character:
const allSameQuotes = 'I\'ve had a perfectly wonderful evening, but this wasn\'t it.';
Branching programs, i.e. ones that do different things if certain conditions are met, rely on if, else if, and else statements in JavaScript. The condition sometimes takes the form of testing whether a result is equal to a value.
This logic is spoken (in English, at least) as “if x equals y, then …” which can literally translate into code using the =, or assignment operator. This leads to unexpected control flow in your program.
As covered in previous challenges, the assignment operator (=) in JavaScript assigns a value to a variable name. And the == and === operators check for equality (the triple === tests for strict equality, meaning both value and type are the same).
The code below assigns x to be 2, which evaluates as true. Almost every value on its own in JavaScript evaluates to true, except what are known as the “falsy” values: false, 0, “” (an empty string), NaN, undefined, and null.
let x = 1; let y = 2; if (x = y) { } else { }
In this example, the code block within the if statement will run for any value of y, unless y is falsy. The else block, which we expect to run here, will not actually run.
When a function or method doesn’t take any arguments, you may forget to include the (empty) opening and closing parentheses when calling it. Often times the result of a function call is saved in a variable for other use in your code. This error can be detected by logging variable values (or their types) to the console and seeing that one is set to a function reference, instead of the expected value the function returns.
The variables in the following example are different:
function myFunction() { return "You rock!"; } let varOne = myFunction; let varTwo = myFunction();
Here varOne is the function myFunction, and varTwo is the string You rock!.
Continuing the discussion on calling functions, the next bug to watch out for is when a function’s arguments are supplied in the incorrect order. If the arguments are different types, such as a function expecting an array and an integer, this will likely throw a runtime error. If the arguments are the same type (all integers, for example), then the logic of the code won’t make sense. Make sure to supply all required arguments, in the proper order to avoid these issues.
Off by one errors (sometimes called OBOE) crop up when you’re trying to target a specific index of a string or array (to slice or access a segment), or when looping over the indices of them. JavaScript indexing starts at zero, not one, which means the last index is always one less than the length of the item. If you try to access an index equal to the length, the program may throw an “index out of range” reference error or print undefined.
When you use string or array methods that take index ranges as arguments, it helps to read the documentation and understand if they are inclusive (the item at the given index is part of what’s returned) or not. Here are some examples of off by one errors:
let alphabet = "abcdefghijklmnopqrstuvwxyz"; let len = alphabet.length; for (let i = 0; i <= len; i++) { console.log(alphabet[i]); } for (let j = 1; j < len; j++) { console.log(alphabet[j]); } for (let k = 0; k < len; k++) { console.log(alphabet[k]); }
The first example here loops one too many times, and the second loops one too few times (missing the first index, 0). The third example is correct.
Sometimes it's necessary to save information, increment counters, or re-set variables within a loop. A potential issue is when variables either should be reinitialized, and aren't, or vice versa. This is particularly dangerous if you accidentally reset the variable being used for the terminal condition, causing an infinite loop.
Printing variable values with each cycle of your loop by using console.log() can uncover buggy behavior related to resetting, or failing to reset a variable.
Loops are great tools when you need your program to run a code block a certain number of times or until a condition is met, but they need a terminal condition that ends the looping. Infinite loops are likely to freeze or crash the browser, and cause general program execution mayhem, which no one wants.
There was an example of an infinite loop in the introduction to this section - it had no terminal condition to break out of the while loop inside loopy(). Do NOT call this function!
function loopy() {
while(true) {
console.log(“Hello, world!”);
}
}
It’s the programmer’s job to ensure that the terminal condition, which tells the program when to break out of the loop code, is eventually reached. One error is incrementing or decrementing a counter variable in the wrong direction from the terminal condition. Another one is accidentally resetting a counter or index variable within the loop code, instead of incrementing or decrementing it.