Editing JS Challenge 2 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) The second code challenge @ DM 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 = [ ['lehlowww', 'hello'], ['dlrwxdonexfe', 'oneworld'], ['oenhodwrol', 'oneworld'], ['rkqodlw', 'world'], ['olaelh', 'hello'], ['helio', 'hell'], ['abcewnorde', 'wonder'], ['wioskrngatwo', 'workingtwo'], ['locaacedo', 'locatedhere'], ['edfbenowaginning', 'beginningnow'], ['CAPS TEST', 'caps test'], ['Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent et mauris magna, ac dapibus ipsum. Sed viverra feugiat vehicula. Pellentesque vel lacinia ipsum. Vestibulum vel diam vel velit mollis adipiscing. Suspendisse potenti. Duis elementum erat vitae neque blandit porttitor. Pellentesque pellentesque vulputate posuere. Pellentesque eget justo elit. Praesent elementum odio sit amet lectus dignissim tincidunt. In quam massa, varius nec convallis quis, luctus sed erat. Cras ut massa metus. Integer lacinia iaculis pretium.', 'luctus sed erat'] ]; var answers = [true, false, true, true, true, false, true, true, false, true, false, true]; // answer key that is case sensitive as it SHOULD be //var answers = [true, false, true, true, true, false, true, true, false, //true, true, 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 for (var int = 0; int < arr.length; int++) { if (answers[int] !== StringScramble(arr[int][0], arr[int][1])) { console.log('Michael K failed test ' + int); throw 'failed test ' + int; } } function StringScramble(clone, orig) { var match = 0 var fail = 0; // var clone = "this is my test string"; var cloneLow = clone.toLowerCase(); var cloneArr = cloneLow.split(""); //var orig = "i miss test"; var origLow = orig.toLowerCase(); var origArr = origLow.split(""); for (var i = 0; i < clone.length; i++) { if (orig.indexOf(clone[i]) == -1) { fail++; } else { match++; } } if (match >= origArr.length) { return true; } else { return false; } } Test 2 Title Async (check if this is an asynchronous test) Code for (var int = 0; int < arr.length; int++) { if (answers[int] !== StringScramble(arr[int][0], arr[int][1])) { console.log('Max H failed test ' + int); throw 'failed test ' + int; } } function StringScramble(str1, str2) { var arr = new Array(26); var index; var arr_length = arr.length; var str1_length = str1.length; var str2_length = str2.length; // Initialize the array to 0 for (var i = 0; i < arr_length; i++) { arr[i] = 0; } str2 = str2.toLowerCase(); // Put char counts into arr for str2 for (var j = 0; j < str2_length; j++) { index = str2[j].charCodeAt(0) - 97; arr[index] = arr[index] + 1; } // Compare str1 to str2 for (var k = 0; k < str1_length; k++) { index = str1[k].charCodeAt(0) - 97; if (arr[index] > 0) arr[index] = arr[index] - 1; } // If any of the elements in the arry is non 0, // there's a mismatch and str1 cannot be a permutation of str2 for (var n = 0; n < arr_length; n++) { if (arr[n] != 0) return false; } return true; } Test 3 Title Async (check if this is an asynchronous test) Code for (var int = 0; int < arr.length; int++) { if (answers[int] !== StringScramble(arr[int][0], arr[int][1])) { console.log('Spencer W failed test ' + int); throw 'failed test ' + int; } } function StringScramble(str1, str2) { var scrambled = str1.split("").sort(); var expected = str2.split("").sort(); var result = []; var ii = scrambled.length; var jj = expected.length; for (var i = 0; i < ii; i++) { for (var j = 0; j < jj; j++) { if (scrambled[i] === expected[j]) { result.push(scrambled[i]); break; } } } var scrambledString = result.toString(); var expectedString = expected.toString(); return (scrambledString === expectedString ? true : false); } Test 4 Title Async (check if this is an asynchronous test) Code for (var int = 0; int < arr.length; int++) { if (answers[int] !== StringScramble(arr[int][0], arr[int][1])) { console.log('Jenni failed test ' + int); throw 'failed test ' + int; } } function StringScramble(str1, str2) { var s1 = str1.split(""), s2 = str2.split(""), l1 = s1.length, l2 = s2.length, a1 = []; // duplicate array accounts for double letters for (var j = 0; j < l1; j++) { if (a1[s1[j]]) { a1[s1[j]]++; } else { a1[s1[j]] = 1; } } for (var i = 0; i < l2; i++) { if (a1[s2[i]] > 0) { a1[s2[i]]--; } else { return false; } } return true; } Test 5 Title Async (check if this is an asynchronous test) Code for (var int = 0; int < arr.length; int++) { if (answers[int] !== StringScramble(arr[int][0], arr[int][1])) { console.log('Frank failed test ' + int); throw 'failed test ' + int; } } function StringScramble(str1, str2) { // Easy fail, if str1 isn't long enough if (str1.length < str2.length) { return false; } // Basic approach: // Sort the letters, then compare them // one by one, and bail out as soon as // we see a letter in s2 that s1 can't supply list1 = str1.split('').sort(); list2 = str2.split('').sort(); var c1 = list1.pop(); var c2 = list2.pop(); while (c1 && c2) { if (c1 == c2) { c1 = list1.pop(); c2 = list2.pop(); } else if (c1 < c2) { // str2 just asked for a char str1 can't provide return false; } else { // burn off excess str1 chars c1 = list1.pop(); } } // a value leftover in c2 means that str1 dried up // before matching all of str2's needs. Fail! return (!c2); } //trues //StringScramble("cat", "act") //StringScramble("cattle", "act") //StringScramble("abbbba", "b") //StringScramble("cattle", "") //StringScramble("", "") //falses //StringScramble("cat", "actor") //StringScramble("cattle", "lettuce") //StringScramble("", "b") Test 6 Title Async (check if this is an asynchronous test) Code for (var int = 0; int < arr.length; int++) { if (answers[int] !== StringScramble(arr[int][0], arr[int][1])) { console.log('Kyle failed test ' + int); throw 'failed test ' + int; } } function StringScramble(str1, str2) { // var lowerstr1 = str1.toLowerCase(); // var lowerstr2 = str2.toLowerCase(); var splitarr1 = str1.split(""); var splitarr2 = str2.split(""); var length1 = splitarr1.length; var length2 = splitarr2.length; var lettercount = new Array(54); var depleted = 0; lettercount['a'] = 0; lettercount['b'] = 0; lettercount['c'] = 0; lettercount['d'] = 0; lettercount['e'] = 0; lettercount['f'] = 0; lettercount['g'] = 0; lettercount['h'] = 0; lettercount['i'] = 0; lettercount['j'] = 0; lettercount['k'] = 0; lettercount['l'] = 0; lettercount['m'] = 0; lettercount['n'] = 0; lettercount['o'] = 0; lettercount['p'] = 0; lettercount['q'] = 0; lettercount['r'] = 0; lettercount['s'] = 0; lettercount['t'] = 0; lettercount['u'] = 0; lettercount['v'] = 0; lettercount['w'] = 0; lettercount['x'] = 0; lettercount['y'] = 0; lettercount['z'] = 0; lettercount['A'] = 0; lettercount['B'] = 0; lettercount['C'] = 0; lettercount['D'] = 0; lettercount['E'] = 0; lettercount['F'] = 0; lettercount['G'] = 0; lettercount['H'] = 0; lettercount['I'] = 0; lettercount['J'] = 0; lettercount['K'] = 0; lettercount['L'] = 0; lettercount['M'] = 0; lettercount['N'] = 0; lettercount['O'] = 0; lettercount['P'] = 0; lettercount['Q'] = 0; lettercount['R'] = 0; lettercount['S'] = 0; lettercount['T'] = 0; lettercount['U'] = 0; lettercount['V'] = 0; lettercount['W'] = 0; lettercount['X'] = 0; lettercount['Y'] = 0; lettercount['Z'] = 0; lettercount[' '] = 0; lettercount['.'] = 0; for (var i = 0; i < length1; i++) { lettercount[splitarr1[i]]++; // alert(splitarr1[i] + " count is " + lettercount[splitarr1[i]]); } for (i = 0; i < length2 ; i++) { depleted = --lettercount[splitarr2[i]]; // alert(splitarr2[i] + " is depleted to " + depleted); if (depleted < 0) { return false; } } return true; } Test 7 Title Async (check if this is an asynchronous test) Code for (var int = 0; int < arr.length; int++) { if (answers[int] !== StringScramble(arr[int][0], arr[int][1])) { console.log('Sam P failed test ' + int); throw 'failed test ' + int; } } function StringScramble(str1, str2) { if (str2.length > str1.length) return false; str1 = str1.split(""); for (var i = 0; i < str2.length; i++) { if ((x = str1.indexOf(str2[i])) == -1) return false; else str1[x] = -1; } return true; } Test 8 Title Async (check if this is an asynchronous test) Code for (var int = 0; int < arr.length; int++) { if (answers[int] !== StringScramble(arr[int][0], arr[int][1])) { console.log('Phil H failed test ' + int); throw 'failed test ' + int; } } function StringScramble(str1, str2) { var pool = str1.split('').sort(); var goal = str2.split('').sort(); for (var i = 0; i < pool.length; i++) { if (pool[i] < goal[0]) continue; if (pool[i] == goal[0]) goal.shift(); if (goal.length == 0) return true; } return false; } Test 9 Title Async (check if this is an asynchronous test) Code for (var int = 0; int < arr.length; int++) { if (answers[int] !== StringScramble(arr[int][0], arr[int][1])) { console.log('Scott failed test ' + int); throw 'failed test ' + int; } } function StringScramble(str1, str2) { var len = str2.length, i = 0, pos; str1 = str1.split(''); for (; i < len; i += 1) { pos = str1.indexOf(str2[i]); if (pos === -1) return false; str1[pos] = '.'; } return true; } Test 10 Title Async (check if this is an asynchronous test) Code for (var int = 0; int < arr.length; int++) { if (answers[int] !== StringScramble(arr[int][0], arr[int][1])) { console.log('Chin failed test ' + int); throw 'failed test ' + int; } } function StringScramble(str1, str2) { for (i = 0; i < str2.length; i++) { pos = str1.indexOf(str2[i]); if (pos === -1) return false; str1 = str1.substr(0, pos) + str1.substr(pos + 1); } return true; } Test 11 Title Async (check if this is an asynchronous test) Code for (var int = 0; int < arr.length; int++) { if (answers[int] !== StringScramble(arr[int][0], arr[int][1])) { console.log('Kun failed test ' + int); throw 'failed test ' + int; } } function StringScramble(str1, str2) { var length = str2.length; if (length > str1.length) { return false; } var indexes = {}; for (i=0; i<length; i++) { var index = str1.indexOf(str2[i], indexes[str2[i]]); if (index == -1) { return false; } else { indexes[str2[i]] = index + 1; } } return true; }