# Messy code and issues in it

**URL:** <https://discourse.processing.org/t/messy-code-and-issues-in-it/7107>\
**Category:** Libraries\
**Created:** [January 2, 2019, 4:38pm UTC](https://discourse.processing.org/t/messy-code-and-issues-in-it/7107 "2019-01-02T16:38:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tojoco](https://avatars.discourse-cdn.com/v4/letter/t/cdc98d/32.png) [@tojoco](https://discourse.processing.org/u/tojoco)\
**Post date:** [January 2, 2019, 4:38pm UTC](https://discourse.processing.org/t/messy-code-and-issues-in-it/7107/1 "2019-01-02T16:38:20Z")

</div>

Im making a program like the amazon echo using p5.speech. My code is not only messy and confusing but every time it hears speech it runs the else instead of what is supposed to happen

```auto
let carl = 'hey carl';
var value = 220;
let time = 'what time is it';

function setup() {
  createCanvas(400, 400);
  let hours = hour();
let min = minute();
  
   if (hours<=12){
    let hr = hour;
    }else{
    let hr = hour%12;
    }

}

function draw() {
  background(value);
  textSize(50);
  //Fill();
  strokeWeight(4);
  text("Hello I am Carl", 35,200);
  textSize(25);

 text("Click to start", 130, 300);
  rect(400,400,0,0);
}

  function mousePressed(){

if(value===220){
value = 0;
  carlBegins();
   } }

function carlBegins() {
let lang = navigator.language || 'en-US';
let speechRec = new p5.SpeechRec(lang, gotSpeech)
speechRec.continuous = true;
speechRec.start();
  let heyCarl = speechRec
  
  function gotSpeech() {
  if (speechRec == carl){
  startLis();
    var myVoice = new p5.Speech();
    myVoice.speak('yes?',hasSpoken);
  }else{
  console.log('oops');
  }
  }
  function startLis() {
 
console.log('yes?')

    times();
    }
    function hasSpoken() {
      if(heyCarl===time){
      
    myVoice.speak(hour);
      myVoice.speak(minute);
      }
      }
}

```

---

<div class="post-metadata">

**Author:** ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)\
**Post date:** [January 2, 2019, 5:53pm UTC](https://discourse.processing.org/t/messy-code-and-issues-in-it/7107/2 "2019-01-02T17:53:16Z")

</div>

Can you please edit your post to format your code?

Also, can you please try to be more specific? Which `if` statement are you having trouble with? Can you narrow the problem down to a simpler example?

---

<div class="post-metadata">

**Author:** ![tojoco](https://avatars.discourse-cdn.com/v4/letter/t/cdc98d/32.png) [@tojoco](https://discourse.processing.org/u/tojoco)\
**Post date:** [January 2, 2019, 6:41pm UTC](https://discourse.processing.org/t/messy-code-and-issues-in-it/7107/3 "2019-01-02T18:41:36Z")

</div>

this is the issue if statement it keeps returning oops instead of what it is supposed to

> [@](#):
>
> function carlBegins() {  
> let lang = navigator.language || ‘en-US’;  
> let speechRec = new p5.SpeechRec(lang, gotSpeech)  
> speechRec.continuous = true;  
> speechRec.start();  
> let heyCarl = speechRec
> 
> function gotSpeech() {  
> if (speechRec == carl){  
> startLis();  
> var myVoice = new p5.Speech();  
> myVoice.speak(‘yes?’,hasSpoken);  
> }else{  
> console.log(‘oops’);  
> }  
> }

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [January 3, 2019, 7:22am UTC](https://discourse.processing.org/t/messy-code-and-issues-in-it/7107/4 "2019-01-03T07:22:19Z")

</div>

pls format your code using the  
Preformatted text block:

```auto
</>

```

( in both of your posts )

pls edit your TITLE

> Messy code and issues in it

to tell the average reader what it is about

> [@tojoco](#):
>
> function gotSpeech() {  
> if (speechRec == carl){

to understand why your IF not work, i suggest  
you print out!

```auto
function gotSpeech() {
  console.log("speechRec: " +speechRec+" carl: "+carl)
}

```

to see whats inside before you make a compare  
you might want to use:  
`speechRec.resultString`  
instead
