Any idea why my JS function isn't working?

  • Thread starter Thread starter Jamin2112
  • Start date Start date
  • Tags Tags
    Function Idea
Click For Summary
SUMMARY

The JavaScript function clock2secs is designed to convert time in the format hh:mm:ss to seconds but initially fails to return the correct value for inputs like "00:02:29" due to incorrect substring parameters and the omission of the radix parameter in parseInt. The function was improved by consolidating error checks into a separate function, isProperTimeForm, and ensuring that all parseInt calls specify a radix of 10. The final implementation successfully passes tests for various time formats, including edge cases like "09:09:09".

PREREQUISITES
  • JavaScript programming fundamentals
  • Understanding of string manipulation in JavaScript
  • Knowledge of the parseInt function and its radix parameter
  • Debugging techniques using browser developer tools
NEXT STEPS
  • Learn about JavaScript string methods, particularly substring and charAt
  • Explore error handling and validation techniques in JavaScript
  • Study the implications of using parseInt without a radix parameter
  • Investigate best practices for writing modular and maintainable JavaScript code
USEFUL FOR

JavaScript developers, particularly those working on time conversion utilities or learning about string manipulation and debugging in JavaScript.

  • #31
I second harborsparrow's recommendation of "JavaScript: The Good Parts". For reference, I also have David Flanagan's "JavaScript: The Definitive Guide", 6th Ed.

Jamin2112, I don't know if you took me up on my suggestion to use the F12 debugging tools (available in pretty much any browser). Debugging your code using these capabilities would provide answers to many of the questions you've asked.

There is also the timeworn method of using print statements to display the values of variables at certain locations in your code. You can do this by adding alert(...) statements in your code.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
5K
Replies
12
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K