Example input
[Source Language]: Python, [Target Language]: JavaScript, [Specify the programming language of the original code]: Python, [Specify the programming language to which the code needs to be translated]: JavaScript, [Paste the relevant code snippet or describe the functionality of the code to be converted]: A Python function that calculates Fibonacci numbers, [Highlight any specific requirements or constraints for the conversion (e.g., performance optimization, library usage, platform compatibility)]: Optimize for browser execution in JavaScript, [Provide any additional context or instructions that will aid in the translation process]: Ensure that the translated code is well-documented,
Example output
To convert a Python Fibonacci number calculator to JavaScript and optimize it for browser execution while ensuring readability and documentation, you can follow these steps:
**Python Code (Source):**
```python
# Fibonacci function in Python
def fibonacci(n):
if n <= 0:
return []
elif n == 1:
return [0]
elif n == 2:
return [0, 1]
else:
fib_list = [0, 1]
while len(fib_list) < n:
next_num = fib_list[-1] + fib_list[-2]
fib_list.append(next_num)
return fib_list
```
**JavaScript Code (Target):**
```javascript
/**
* Calculate Fibonacci numbers in JavaScript.
* @param {number} n - The number of Fibonacci numbers to generate.
* @returns {Array} An array containing the first n Fibonacci numbers.
*/
function fibonacci(n) {
if (n <= 0) {
return [];
} else if (n === 1) {
return [0];
} else if (n === 2) {
return [0, 1];
} else {
const fibList = [0, 1];
while (fibList.length < n) {
const nextNum = fibList[fibList.length - 1] + fibList[fibList.length - 2];
fibList.push(nextNum);
}
return fibList;
}
}
```
**Explanation:**
1. The Python code defines a Fibonacci function that calculates the first `n` Fibonacci numbers and returns them as a list. We'll maintain the same logic in the JavaScript translation.
2. We use JavaScript's `function` keyword to define a function called `fibonacci` that takes a parameter `n`, representing the number of Fibonacci numbers to generate.
3. In JavaScript, we use `===` for strict equality comparison, so we replace `==` with `===` to ensure correct comparisons.
4. We replace the Python `elif` with JavaScript's `else if`.
5. The code uses the same logic for Fibonacci number generation in both languages. We initialize the `fibList` array with `[0, 1]` and then use a `while` loop to add new Fibonacci numbers until the list reaches the desired length.
6. We update variable names to follow JavaScript naming conventions, using `camelCase` for variables (`fibList`, `nextNum`) and using `const` to declare constants.
7. To make the JavaScript code well-documented, we add a JSDoc comment that describes the function's purpose, parameters, and return value.
This JavaScript code should be efficient for browser execution and maintain the functionality and readability of the original Python code. It also includes proper documentation to help other developers understand the code's purpose and usage.