jeudi 30 mai 2019

same numbers, different input methods, different outputs

While bubble sorting an array, it will give different outputs depending on the method of input, manually it sorts correctly, random it won't. Any ideas?

I've already tried doing the same inputs manually as it does at random but it still didn't work for random.

    valores =["20", "78", "48", "55", "4"] 

(same input in both functions)

let valores = []

function randomVals() {
    let cantidad
    valores = []

    if (rCantidad.value != '') {
        cantidad = Number(rCantidad.value)
    } else {
        cantidad = 15
    }

    for (let k = 0; k < cantidad; k++) {
        valores.push(((Math.random()) * 100).toFixed(0))
    }
    console.log(valores)
}


function agregarValor() {

    let val = Number(valor.value)

    valores.push(val)

    console.log(valores)

}

expected sorted output:

     valores=[4, 20, 48, 55, 78]

sorted output given while using

      randomVals() valores=["20", "4", "48", "55", "78"]




Aucun commentaire:

Enregistrer un commentaire