Editing Identifying Octal Strings This edit will create a new revision. Your details (optional) Name Email (won’t be displayed; might be used for Gravatar) URL Test case details Title * Published (uncheck if you want to fiddle around before making the page public) Description (in case you feel further explanation is needed)(Markdown syntax is allowed) Each test must return true for all numbers that would be interpreted by major web browsers as an octal number, but in the string type. Remember that all acceptable octal numbers begin with any number of zeroes (i.e. "0..."). Furthermore, remember that a negative octal number would lead with a negative sign (i.e. "-0..."). Important note: There MUST be an assumption that the string does contain a number! This means that simply searching for the number "0" at the beginning of the string (allowing for a negative sign) is sufficient. Second important note: Since the string is unlikely to be an octal string, I have tested for "false" a greater number of times than true. Third important note: Treating numbers beginning with "0" as an octal number does not appear to be part of the official specification, despite the default behaviour of popular web browsers. Are you a spammer? (just answer the question) Preparation code Preparation code HTML (this will be inserted in the <body> of a valid HTML5 document in standards mode) (useful when testing DOM operations or including libraries) Include JavaScript libraries as follows: <script src="//cdn.ext/library.js"></script> Define setup for all tests (variables, functions, arrays or other objects that will be used in the tests) (runs before each clocked test loop, outside of the timed code region) (e.g. define local test variables, reset global variables, clear canvas, etc.) (see FAQ) var octalString = "012345"; var nonOctalString = "12345"; var longOctalString = "-00123456789123456789"; var longNonOctalString = "123456789123456789"; function charAt(str) { if (str.charAt(0) === "0" || (str.charAt(1) === "0" && str.charAt(0) === "-")) { return true; }; }; function regExp(str) { if (/^0/.test(str)) { return true; }; }; function indexOf(str) { var num = str.indexOf("0"); if (num === 0 || (num === 1 && str.indexOf("-") === 0)) { return true; }; }; function indexOfTwo(str) { var num = str.indexOf("0"); if (num === 0 || (num === 1 && str < 0)) { return true; }; }; Define teardown for all tests (runs after each clocked test loop, outside of the timed code region) (see FAQ) Code snippets to compare Test 1 Title Async (check if this is an asynchronous test) Code charAt(octalString); charAt(longOctalString); charAt(nonOctalString); charAt(longNonOctalString); charAt(nonOctalString); charAt(longNonOctalString); charAt(nonOctalString); charAt(longNonOctalString); Test 2 Title Async (check if this is an asynchronous test) Code regExp(octalString); regExp(longOctalString); regExp(nonOctalString); regExp(longNonOctalString); regExp(nonOctalString); regExp(longNonOctalString); regExp(nonOctalString); regExp(longNonOctalString); Test 3 Title Async (check if this is an asynchronous test) Code indexOf(octalString); indexOf(longOctalString); indexOf(nonOctalString); indexOf(longNonOctalString); indexOf(nonOctalString); indexOf(longNonOctalString); indexOf(nonOctalString); indexOf(longNonOctalString); Test 4 Title Async (check if this is an asynchronous test) Code indexOfTwo(octalString); indexOfTwo(longOctalString); indexOfTwo(nonOctalString); indexOfTwo(longNonOctalString); indexOfTwo(nonOctalString); indexOfTwo(longNonOctalString); indexOfTwo(nonOctalString); indexOfTwo(longNonOctalString);