Skip to content

Instantly share code, notes, and snippets.

@luxarts
Last active May 25, 2020 20:22
Show Gist options
  • Save luxarts/f9fce67f3c45d6a57113d9ef58f848a0 to your computer and use it in GitHub Desktop.
Save luxarts/f9fce67f3c45d6a57113d9ef58f848a0 to your computer and use it in GitHub Desktop.
let A = [3,8,2,3,3,2];
let B = [7,1,2,8,2];
let C = [3,1,4,1,5];
let D = [5,5,5,5,5];
let E = [0];
let F = [1];
let G = [2,2,2,1];
/* @brief Obtiene el mayor número de una lsita que se repite a si mismo la misma cantidad de veces que su valor.
* @param list: Lista de números enteros
* @return Mayor número
*/
function getMatchValue(list) {
let appearanceCounter = 0;
// Ordena los números de mayor a menor
const orderedList = list.sort( (a,b) => b-a);
// Guarda el primer número
let previousValue = orderedList[0];
// Recorre todos los números
for (let i = 0; i<orderedList.length; i++){
// El número cambió
if (orderedList[i] != previousValue){
// Resetea el contador
appearanceCounter = 1;
previousValue = orderedList[i];
} else {
// Incrementa el contador
appearanceCounter++;
// Si el contador llegó al valor del número
if (appearanceCounter == orderedList[i]){
return orderedList[i];
}
}
}
// Ningun número cumple
return 0;
}
function test(actual, expected){
if (actual != expected){
console.error(`FAIL! Expected ${expected}, actual ${actual}`);
} else {
console.log(`PASSED!`);
}
}
test(getMatchValue(A), 3);
test(getMatchValue(B), 2);
test(getMatchValue(C), null);
test(getMatchValue(D), 5);
test(getMatchValue(E), 0);
test(getMatchValue(F), 1);
test(getMatchValue(G), 0);
@lucasdellasala
Copy link

lucasdellasala commented May 21, 2020

`let A = [3,8,2,3,3,2];
let B = [7,1,2,8,2];
let C = [3,1,4,1,5];
let D = [5,5,5,5,5];
let E = [1];
let F = [0];

function getMatchValue(list) {
let appearanceCounter = 1;

// Ordena los números de mayor a menor
const orderedList = list.sort( (a,b) => b-a);

// Guarda el primer número
let previousValue = orderedList[0];

// Recorre todos los números
for (let i = 0; i<orderedList.length; i++){
    // El número cambió
    if (orderedList[i] != previousValue){
        // Resetea el contador
        appearanceCounter = 1;
        previousValue = orderedList[i];
    } else {
        // Incrementa el contador
        appearanceCounter++;
        // Si el contador llegó al valor del número
        if (appearanceCounter == orderedList[i]){
            return orderedList[i];
        }
    }
}

// Ninguna número cumple
return 0;

}

function test(actual, expected){
if (actual != expected){
console.error(FAIL! Expected ${expected}, actual ${actual});
} else {
console.log(PASSED!);
}
}

test(getMatchValue(A), 3);
test(getMatchValue(B), 2);
test(getMatchValue(C), 0);
test(getMatchValue(D), 5);
test(getMatchValue(E), 1);
test(getMatchValue(F), 0);`

### No pasa el test E

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment