Editing scramble 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) 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 arr = ['aabecaa', 'abbbaac', 'aabejiabkfabed', '123224']; var answers = ['yes aa', 'no null', 'yes abe', 'no null']; 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 for (var i = 0; i < arr.length; i++) { if (answers[i] !== PatternChaser(arr[i])) { console.log('Sam P failed test ' + i); throw 'failed test ' + i; } } function PatternChaser(str) { var i = 0; var strLength = str.length; var ptrMaxLength = Math.floor(strLength / 2); while (ptrMaxLength > 1) { testLength = 1 + strLength - (ptrMaxLength * 2); for (; i < testLength; i += 1) { if (str.indexOf(str.substr(i, ptrMaxLength), i + ptrMaxLength) > -1) { return "yes " + str.substr(i, ptrMaxLength); } } ptrMaxLength -= 1; i = 0; } return "no null"; } Test 2 Title Async (check if this is an asynchronous test) Code for (var i = 0; i < arr.length; i++) { if (answers[i] !== PatternChaser(arr[i])) { console.log('Sam P failed test ' + i); throw 'failed test ' + i; } } function PatternChaser(str) { var i = 0; var strLength = str.length; var ptrMaxLength = Math.floor(strLength / 2); var maxPosLength = (strLength + 1) - (ptrMaxLength + ptrMaxLength); while (ptrMaxLength > 1) { var testStr, passIndex = null; for (i = 0; i < maxPosLength; i += 1) { testStr = str.substr(i, ptrMaxLength); passIndex = i + ptrMaxLength - 1; if (str.lastIndexOf(testStr) > passIndex) { return "yes " + testStr; } } ptrMaxLength -= 1; maxPosLength += 2; } return "no null"; } Test 3 Title Async (check if this is an asynchronous test) Code for (var i = 0; i < arr.length; i++) { if (answers[i] !== PatternChaser(arr[i])) { console.log('Sam P failed test ' + i); throw 'failed test ' + i; } } function PatternChaser(str) { var strLength = str.length; var ptrMaxLength = Math.floor(strLength / 2); var maxPosLength = (strLength + 1) - (ptrMaxLength + ptrMaxLength); while (ptrMaxLength > 1) { var i, testStr, passIndex = null; for (i = 0; i < maxPosLength; i += 1) { testStr = str.substr(i, ptrMaxLength); passIndex = i + ptrMaxLength - 1; if (str.lastIndexOf(testStr) > passIndex) { return "yes " + testStr; } } ptrMaxLength -= 1; maxPosLength += 2; } return "no null"; } Test 4 Title Async (check if this is an asynchronous test) Code for (var i = 0; i < arr.length; i++) { if (answers[i] !== PatternChaser(arr[i])) { console.log('Sam P failed test ' + i); throw 'failed test ' + i; } } function PatternChaser(str) { var i = 0; var strLength = str.length; var ptrMaxLength = Math.floor(strLength / 2); var maxPosLength = (strLength + 1) - (ptrMaxLength + ptrMaxLength); while (ptrMaxLength > 1) { for (i = 0; i < maxPosLength; i += 1) { testStr = str.substr(i, ptrMaxLength); passIndex = i + ptrMaxLength - 1; if (str.lastIndexOf(testStr) > passIndex) { return "yes " + testStr; } } ptrMaxLength -= 1; maxPosLength += 2; } return "no null"; } Test 5 Title Async (check if this is an asynchronous test) Code for (var i = 0; i < arr.length; i++) { if (answers[i] !== PatternChaser(arr[i])) { console.log('Sam P failed test ' + i); throw 'failed test ' + i; } } function PatternChaser(str) { var strLength = str.length; var ptrMaxLength = Math.floor(strLength / 2); var maxPosLength = (strLength + 1) - (ptrMaxLength + ptrMaxLength); var yes = "yes "; var no = "no null"; while (ptrMaxLength > 1) { var i, testStr, passIndex = null; for (i = 0; i < maxPosLength; i += 1) { testStr = str.substr(i, ptrMaxLength); passIndex = i + ptrMaxLength - 1; if (str.lastIndexOf(testStr) > passIndex) { return yes + testStr; } } ptrMaxLength -= 1; maxPosLength += 2; } return no; }