Skip to content

Instantly share code, notes, and snippets.

@atorralb
Last active June 7, 2016 17:52
Show Gist options
  • Save atorralb/5d9cc75ceacd19c1548c874c6f9b7867 to your computer and use it in GitHub Desktop.
Save atorralb/5d9cc75ceacd19c1548c874c6f9b7867 to your computer and use it in GitHub Desktop.
/*
function identifyOrder(order_string){
var order =[];
for (var i = 0; i < order_string.length; i++) {
order.push(order_string.charAt(i));
}
return order;
}
function ordenar_extraterrestre(desordenadas, orden_alfabeto){
// var array = ['revestir', 'miel', 'extraterrestre', 'auto', 'automovil', 'al'];
// return array;
var ordenadas = [];
var order = identifyOrder(orden_alfabeto);
order.forEach(function(key) {
var found = false;
list = desordenadas.filter(function(item) {
if(!found && item[1] == key) {
ordenadas.push(item);
found = true;
return false;
} else
return true;
})
})
ordenadas.forEach(function(item) {
return (item[0]) /// Bob Jason Henry Thomas Andrew
})
}
console.log(ordenar_extraterrestre(lista, alfabeto));
*/
//create the alphabet and assign each letter a number
//we create a function that can assign a value to each letter according to the input/order provided
//function to get object size for future use
Object.size = function(obj) {
var size = 0, key;
for (key in obj) {
if (obj.hasOwnProperty(key)) size++;
}
return size;
};
function rosettaStone(order_string){
var alphabet_values = new Object();
for (var i = 0; i < order_string.length; i++) {
alphabet_values[order_string.charAt(i)] = i;
}
return alphabet_values;
}
//extract items/words from array and convert them into numeric arrays
function splitWords(theArray) {
var newList = [];
for (var i = 0; i < theArray.length; i++) {
// i= 1 miel
for (var x = 0; x < theArray[i].length; x++) {
newList[i]= theArray[i].split('');
}
}
return newList;
}
var alfabeto = 'zyxwvutsrqponmlkjihgfedcba';
var lista = ['miel', 'extraterrestre', 'al', 'automovil', 'auto', 'revestir'];
function convertNumeric(){
var sortingArr = rosettaStone(alfabeto);
var sortingSize = Object.size(sortingArr);
var itemsArr = splitWords(lista);
var sortedArray = [];
//use this for nodejs
/*
Object.keys(sortingArr).forEach(function(key) {
var val = sortingArr[key];
console.log(val);
});
*/
//console.log(Object.getOwnPropertyNames(sortingArr)[1]);
var words = itemsArr;
for (var i =0; i < words.length; i++){
for (var n = 0, len = words[i].length; n < len; n++) {
console.log(words[i][n]);
for ( var u =0; u < sortingSize; u++){
if (words[i][n] === Object.getOwnPropertyNames(sortingArr)[u]){
console.log(sortingArr[Object.getOwnPropertyNames(sortingArr)[u]])
}
}
}
}
//console.log(sortedArray);
}
convertNumeric();
/*
var alien_rosetta = {z: 1, y: 2, x: 3, w: 4, v: 5, u: 6, t: 7, s: 8, r: 9, q: 10, p: 11, o:12, n: 13, m:14, l:15, k:16, j: 17, i: 18, h: 19, g: 20, f: 21, e: 22, d: 23, c: 24, b: 25, a: 26};
//convert each array item (word) into its numeric value
// "miel" = [13,9,5,12]
//compare each array/word to see which one goes before which one eg:
//we can see here that automovil has a smaller second (automovil[1]) number than al (al[1])
//"automovil" = [26, 6.....]
//"al" = [26, 15]
list = lista.filter(function(item){
console.log(item);
});
var i;
for(i=0; i < lista.length; i++){
console.log(lista[i].charAt(0));
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment