1

At the first line of my code I put var array = [] but I get the error pointing towards this function. If I log array before calling the function its exactly what it's supposed to be but if I do that anywhere inside the function it doesn't get logged before the error

let elements = 25
var array = []
let goal = []
let tempAr = []
let i = 1
 
function setup() {
 
    // put setup code here
    createCanvas(600, 600)
    background(25)
    // make the goal array ascending
    while (goal.length < elements) {
        goal.push(i)
        i++
    }
 
    // make the scrambled array
 
    i = 0
    tempAr = goal
    while (i < elements) {
        let rng = Math.floor(random(tempAr.length))
        array.push(tempAr[rng])
        tempAr.splice(rng, 1)
 
        i++
 
    }
 
}
 
function draw() {
    fill('#f1f442')
 
    drawRect()
    sort()
 
}
 
 
function drawRect() {
    i = 1
    while (i <= elements) {
        rect(i * (width / elements) - (width / elements), height - array[i - 1] * height / elements, width / elements, array[i - 1] * height / elements)
        i++
    }
}
 
function sort() {
 
    let sorted = false
 
    while (!sorted) {
 
        sorted = true
        var e = 0
        while (e < array.length) {
 
            if (array[e] > array[e + 1]) {
                let temp = array[e + 1]
                array[e + 1] = array[e]
                array[e] = temp
                sorted = false
 
            }
            drawRect()
            e++
        }
 
    }
 
 
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.3/p5.js"></script>
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Stakoter
  • 17
  • 5

1 Answers1

0

sort() is the name of a built-in function in p5.js.
You've to choose a different name for your function. So rename sort to something else (e.g. mysort) to make your code run.

let elements = 25
var array = []
let goal = []
let tempAr = []
let i = 1
 
function setup() {
 
    // put setup code here
    createCanvas(600, 600)
    background(25)
    // make the goal array ascending
    while (goal.length < elements) {
        goal.push(i)
        i++
    }
 
    // make the scrambled array
 
    i = 0
    tempAr = goal
    while (i < elements) {
        let rng = Math.floor(random(tempAr.length))
        array.push(tempAr[rng])
        tempAr.splice(rng, 1)
 
        i++
 
    }
 
}
 
function draw() {
    fill('#f1f442')
 
    drawRect()
    mysort()
 
}
 
 
function drawRect() {
    i = 1
    while (i <= elements) {
        rect(i * (width / elements) - (width / elements), height - array[i - 1] * height / elements, width / elements, array[i - 1] * height / elements)
        i++
    }
}
 
function mysort() {
 
    let sorted = false
 
    while (!sorted) {
 
        sorted = true
        var e = 0
        while (e < array.length) {
 
            if (array[e] > array[e + 1]) {
                let temp = array[e + 1]
                array[e + 1] = array[e]
                array[e] = temp
                sorted = false
 
            }
            drawRect()
            e++
        }
    }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.3/p5.js"></script>
Rabbid76
  • 202,892
  • 27
  • 131
  • 174