-
Notifications
You must be signed in to change notification settings - Fork 8
10. JavaScript standards
Alex Parry edited this page Mar 20, 2025
·
4 revisions
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}` |
- 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 test_items = [10,11,13,15,18,25,29]
let first_index = 0
let last_index = test_items.length - 1
console.log(test_items)
// Search for a value and return the found index
let item_to_find = 18
console.log(`\nThe search item is ${item_to_find}`)
let index = binarySearchRecursive(test_items, item_to_find, first_index, last_index)
if (index == -1) {
console.log(`\n${item_to_find} was not found in the list`)
} else {
console.log(`\n${item_to_find} 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.