Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JavaScript code samples #13

Open
jakeg opened this issue Dec 6, 2024 · 4 comments
Open

JavaScript code samples #13

jakeg opened this issue Dec 6, 2024 · 4 comments

Comments

@jakeg
Copy link

jakeg commented Dec 6, 2024

I'm here atm with @dianedowling at your offices (I'm from Cambridge Maths School). If I submit some JavaScript code samples, can they be included on the website? Please let me know any details to follow re formatting etc for pull requests to be accepted.

@alexparry
Copy link
Collaborator

Hi Jake, sorry for taking so long to respond. We would be very happy if you wanted to submit some JavaScript samples. Currently, we have a Wiki on our coding standards but we would need to add one for JavaScript. However, it should still give you an indication of the general approaches we take: https://github.com/raspberrypilearning/ada-code-samples/wiki

@jakeg
Copy link
Author

jakeg commented Mar 5, 2025

I'll try to start some time soon with writing a coding standards page with a couple of code samples, so that we can agree on a coding standard for JS first.

@alexparry
Copy link
Collaborator

Thanks Jake, much appreciated!

@jakeg
Copy link
Author

jakeg commented Mar 6, 2025

Attached is an example JavaScript-standards.md file

12.-JavaScript-standards.md

Things are a bit complicated with JavaScript when it comes to having a main() procedure which is run when the file is executed directly, as well as exporting functions/classes. It is particularly complicated by Node (being the most common non-browser JavaScript runtime) using require() as opposed to more modern import/export modules. This is all made much easier with the Bun and Deno runtimes (at my school we only use Bun).

Below (which is also in the attached markdown) is an example program that has a function which is exported:

// Add two numbers
function add (a, b) {
  let sum = a + b
  return sum
}

// The main method is the entry point when executed directly
function main () {
  let sum = add(5, 7)
  console.log(`The result of the addition was ${sum}`)
}

// This code will run if this file is executed directly
// (i.e. not called by another program)
if (import.meta.main) {
  main()
}

// Export the function for use in other programs
export default add

And an example which doesn't have an export, for a simple program:

let sum = 0

for (let i = 0; i < 5; i++) {
  sum += i
}

console.log(`The sum is ${sum}`)

Any thoughts on my suggestions for how to handle main()/exports? Or should we just ignore any exports, and always just call main()? Or not bother with a main() procedure at all, and just have the code that's currently in there at the top-level instead? eg instead of the first code sample above, having this:

let sum = add(5, 7)
console.log(`The result of the addition was ${sum}`)

// Add two numbers
function add (a, b) {
  let sum = a + b
  return sum
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants