-
Notifications
You must be signed in to change notification settings - Fork 8
10. JavaScript standards
The following code is from our Ada Computer Science content page on the binary search algorithm (recursive), which includes a detailed explanation of the algorithm.
// Performs a binary search recursively
function binarySearchRecursive (items, searchItem, first, last) {
// Base case for recursion: The recursion will stop when the
// index of the first item is greater than the index of last
if (first > last) {
return -1 // Return -1 if the search item is not found
}
// Recursively call the function
else {
// Find the midpoint position (in the middle of the range)
let midpoint = (first + last) / 2
// Compare the item at the midpoint to the search item
if (items[midpoint] == searchItem) {
// If the item has been found, return the midpoint position
return midpoint
}
// Check if the search item is greater than the item at the midpoint
else if (searchItem > items[midpoint]) {
// Focus on the items after the midpoint
first = midpoint + 1
return binarySearchRecursive(items, searchItem, first, last)
}
// Otherwise the item at the midpoint is greater than the search item
else {
// Focus on the items before the midpoint
last = midpoint - 1
return binarySearchRecursive(items, searchItem, first, last)
}
}
}
In general or when in doubt, follow StandardJS rules.
Guidance | Example | |
---|---|---|
Class | Concept name in PascalCase | BinarySearch |
Methods | Name should reflect the purpose of the method in camelCase | binarySearchRecursive |
Parameters | camelCase | totalPay |
Variables | camelCase | totalPay |
Constants | Constants should be declared const and use camelCase |
camelCase |
Strings | Prefer single quotes | 'Hello world' |
Strings | Use double quotes to avoid escaping | "Bob's cake" |
Strings | Use backticks when using variables | `The totalPay is ${totalPay}` |
Use let
to declare new variables rather than var
for consistency across the code examples. Be aware that the scope of let
and var
is different:
-
let
variables are only available inside the block that they are defined in -
var
variables are available throughout the function that they are defined in
- Two spaces for indentation
- Single space between closing parentheses and new curly brace
- Single space between function identifier and parameter list
- Single space either side of operators
- Single space after commas in arrays and lists
- Two lines between functions (or methods)
- Use white space (single line) within function if it helps readability
- Open curly brace goes on the same line as preceding construct (method, if statement, loop, etc), with a space between closing parentheses and new curly brace
- Closing brace goes on a new line
- Single line comment (
//
) above function with important information about the function - All comments: Space after "
//
", capital letter at start and no full stop
Example: console.log(`The sum of 1 to ${n} is: ${result}`)
/*
Raspberry Pi Foundation
Developed as part of Ada Computer Science
Usage licensed under CC BY-NC-SA 4.0
*/
Often the exemplar code in the GitHub repo contains extra code for calling the function(s) with test data. In JavaScript, it is not standard practice to include a main()
function like it is in some other languages. Therefore, any calls to the function(s) for testing should be made just before the first function definition. For example:
// Testing: Perform a binary search on the test data
let testItems = [10, 11, 13, 15, 18, 25, 29]
let firstIndex = 0
let lastIndex = testItems.length - 1
console.log(testItems)
// Search for a value and return the found index
let itemToFind = 18
console.log(`The search item is ${itemToFind}`)
let index = binarySearchRecursive(testItems, itemToFind, firstIndex, lastIndex)
if (index == -1) {
console.log(`${itemToFind} was not found in the list`)
} else {
console.log(`${itemToFind} was found at index ${index}`)
}
// Performs a binary search recursively
function binarySearchRecursive (items, searchItem, first, last) {
// ...
}
Thank you to Jake Gordon for his contributions with creating the JavaScript standards.