Last active
January 6, 2019 11:40
-
-
Save SandyWyper/b6885412273f18925e6a6e5a8966e315 to your computer and use it in GitHub Desktop.
Having issues with something I'm working on. This sinippet comes from the very end of my solution to this CodeWars problem ..... https://www.codewars.com/kata/strings-mix/train/javascript.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// In this challenge you are given two stings. You must take the letters from both strings and list them in a spacific order. | |
// I've extracted the letters, counted instances and filtered out ones not required. I am left with an array such as this. | |
// Each element of the array contains three values. | |
// [ 1, 2, 3 ]: | |
// 1. The number of the string that had the most of that letter. If both strings had the same number then an '=' is used. | |
// 2. The letter. | |
// 3. The number of instances that letter occured. | |
const arrayToSort = [ [ '2', 'b', 4 ], | |
[ '=', 'd', 2 ], | |
[ '2', 'f', 2 ], | |
[ '2', 'g', 2 ], | |
[ '=', 'k', 2 ], | |
[ '1', 'm', 2 ], | |
[ '1', 'p', 2 ], | |
[ '2', 'q', 2 ], | |
[ '1', 's', 3 ], | |
[ '1', 'u', 2 ], | |
[ '1', 'w', 2 ], | |
[ '1', 'x', 3 ] ]; | |
let sortedArray = sortArrays(arrayToSort); | |
// The array must be sorted by the number of occurences first ( index 2 ). If that is even, then by the string it came from, | |
//( index 0 ), "1", then "2", then "=". Finally if those are even it should be sorted by char code point of the | |
// letter, ( index 1 ). | |
function sortArrays(arr){ | |
let sorted = arr.sort(function(a, b) { | |
if(a[2] < b[2]) { return 1; } | |
if(a[2] > b[2]) { return -1; } | |
if(a[2] === b[2]) { | |
if(a[0] > b[0]) { return 1; } | |
if(b[0] < b[0]) { return -1; } | |
if(a[0] === b[0]) { | |
if(a[1].codePointAt(0) > b[1].codePointAt(0)) { return 1; } | |
if(a[1].codePointAt(0) < b[1].codePointAt(0)) { return -1; } | |
} | |
} | |
}); | |
return sorted; | |
} | |
// This sort function works most of the time, but there are 110 test cases and only passes 81. When I stick a for loop | |
// on the sort function and run it 200 times it gets up to 97 passes. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Woops! Typo on line 33.
if(b[0] < b[0]) { return -1; }
Works now!