1

I am experienced in python but completely new to java. I am using p5 and want to set up a simple function that, depending on what number the user inputs, it draws that many circles. I'm not sure why it is not working.

var numProton;

function setup() {
  numProton = createInput();
  numProton.changed(nucleus);
  createCanvas(600, 500);
  background(255);  
}

function draw() {
    noStroke()
    textSize(15);
    fill(0, 0, 0);
    text('^ # of Protons', 25, 30);
    text('^ # of Neutrons', 150, 30);
    text('^ # of Electrons', 275, 30);
}

function nucleus() {
    var i = 0;
    while(i <= numProton.value) {
        ellipse(300, 250, 10);
        i++;
    }
}

Probably a very simple error but I appreciate the help none the less.

reisnern21
  • 107
  • 2
  • 9
  • What exactly is happening right now? Does it draw any circles at all? – inavda Oct 12 '18 at 01:58
  • `console.log(+numProton.value)` - what is the output in the developer tools console? – Jaromanda X Oct 12 '18 at 02:17
  • Where is nucleus being called from? The function draw will be called over and over in p5.js so if you arn't calling nucleus from there, that function won't be executed. Put a console.log('test') in nucleus to see if it is being called at all. – Vigrant Oct 12 '18 at 02:20

2 Answers2

2

Why don't you use a for loop, it has the same exact purpose:

for (var i = 0; i <= numProton.value; i++) {
    ellipse(300, 250, 10);
}
Da Mahdi03
  • 1,468
  • 1
  • 9
  • 18
  • if the while loop doesn't work, why would a for loop? – Jaromanda X Oct 12 '18 at 02:16
  • @JaromandaX What if the problem is with the `while` loop? In JavaScript, things like this have happened to me for weird reasons, where even if I copy and paste from the Documentation, it just refuses to work, so I look for another way! – Da Mahdi03 Oct 12 '18 at 02:18
  • The logic is identical. If you can show such a simple for loop working where a equivalent while loop fails I'll be impressed – Jaromanda X Oct 12 '18 at 02:22
  • 1
    `things like this have happened to me for weird reasons` - I take it you can't recall any of them? – Jaromanda X Oct 12 '18 at 02:29
  • @JaromandaX No, it's that I just never found out, I fixed the problem using a workaround. It's happened to me a couple of times. – Da Mahdi03 Oct 12 '18 at 02:30
2

Is it possible that numProton.value() is supposed to be a function call with ()?

See here p5.js/changed

shmuels
  • 1,039
  • 1
  • 9
  • 22