# How can i optimize this chain of if/else statement?

**URL:** <https://discourse.processing.org/t/how-can-i-optimize-this-chain-of-if-else-statement/22182>\
**Category:** Coding Questions\
**Created:** [June 26, 2020, 4:49pm UTC](https://discourse.processing.org/t/how-can-i-optimize-this-chain-of-if-else-statement/22182 "2020-06-26T16:49:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bismarck](https://avatars.discourse-cdn.com/v4/letter/b/c67d28/32.png) [@bismarck](https://discourse.processing.org/u/bismarck)\
**Post date:** [June 26, 2020, 4:49pm UTC](https://discourse.processing.org/t/how-can-i-optimize-this-chain-of-if-else-statement/22182/1 "2020-06-26T16:49:56Z")

</div>

hello i want to optimize this code.  
I thought that i can use the switch statement, but i don’t know how to implement it:

```auto
//function setup() {
  createCanvas(400, 400);
}

function draw() {
  background(220);
  
  let s = second();
  let mt = minute();
  let h = hour();
  translate(-100,150);

  //check if the second/minute/hour need a 0 before
  if(h < 10 && mt < 10 && s < 10){
    text('0'+h+'/'+'0'+mt+'/'+'0'+s,360,0);
  }
  else if(h < 10 && mt < 10){
    text('0'+h+'/'+'0'+mt+'/'+s,360,0);
  }
  else if(s < 10 && mt < 10){
    text(h+'/'+'0'+mt+'/'+'0'+s,360,0);
  }
  else if(h < 10 && s < 10){
    text('0'+h+'/'+mt+'/'+'0'+s,360,0);
  }
  else if(h < 10){
    text('0'+h+'/'+mt+'/'+s,360,0);
  }
  else if(mt < 10){
    text(h+'/'+'0'+mt+'/'+s,360,0);
  }
  else if(s < 10){
    text(h+'/'+mt+'/'+'0'+s,360,0);
  }
  else{
    text(h+'/'+mt+'/'+s,360,0);
  }
}

```

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [June 26, 2020, 5:00pm UTC](https://discourse.processing.org/t/how-can-i-optimize-this-chain-of-if-else-statement/22182/2 "2020-06-26T17:00:34Z")

</div>

Hi, you can use the [nf()](https://p5js.org/reference/#/p5/nf) function to format your string.

---

<div class="post-metadata">

**Author:** ![bismarck](https://avatars.discourse-cdn.com/v4/letter/b/c67d28/32.png) [@bismarck](https://discourse.processing.org/u/bismarck)\
**Post date:** [June 26, 2020, 5:03pm UTC](https://discourse.processing.org/t/how-can-i-optimize-this-chain-of-if-else-statement/22182/3 "2020-06-26T17:03:01Z")

</div>

thank you for your help i try to use it right now

---

<div class="post-metadata">

**Author:** ![Hyperion65](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hyperion65/32/10569_2.png) [@Hyperion65](https://discourse.processing.org/u/Hyperion65)\
**Post date:** [June 27, 2020, 9:07pm UTC](https://discourse.processing.org/t/how-can-i-optimize-this-chain-of-if-else-statement/22182/4 "2020-06-27T21:07:02Z")

</div>

```auto
let times = [];
//---------------------------------------
function setup() {
  createCanvas(400, 400);
}
//---------------------------------------
function draw() {
  background(220);
  times[0] = hour();
  times[1] = minute();
  times[2] = second();
  times = nf(times, 2);
  //-------------------
  text(times, 10, 30);  
  text(times[0]+" : "+times[1]+" : "+times[2],10,60);
}

```

as he said 🙂 could look like this…  
since nf can gobble up arrays too, it might make sense to use one.
