1

I have taken advice from previous questions I asked about adding mp3 files to my javascript code and came up with this.

var howrutday = new Audio('file:///C:/This_PC/Music/Science_Fair_Robot/howrutday.mp3');


var name = function (robot) {
    if ('Hi.' + 'Hi!' + 'Hello.' + 'Hello!' + 'Greetings.' + 'Greetings!') {
    console.log("How are you doing today " + name + "?");
    howrutday.play();
}   else if ('Good morning.' + 'Good morning!') {
    console.log("How are you doing today " + name + "?");
}   else if ('Good afternoon.' + 'Good afternoon!') {
    console.log("And to you also, what a lovely day.");
}   else if ('Good evening.' + 'Good evening!') {
    console.log("How was your day?");
}   else if ('Good night.' + 'Good night!') {
    console.log("Rest well " + name + ". I hope to see you tomorrow.");
}   else {
    console.log("Try saying that again but make sure you are using proper grammar like applying punctuation and capitalization.");
}

I'm not sure if it's a problem with my syntax or if I'm not making my audio file visible to the program. Also does anybody have recommendations for websites to run my code in because maybe the one I'm using does not play audio. Just a theory though.

jc92me
  • 27
  • 1
  • 3
  • 7
  • You've got a long way to go, cause I'm assuming you don't know HTML, JS, or CSS very well. Sorry if I'm wrong but I'm trying to establish and assess your knowledge. Your question requires an answer of huge proportions. Do you have experience in another language like C, C++, or Java? – zer00ne Nov 15 '15 at 23:34
  • No, I'm not telling him to do that, read what I'm saying. – zer00ne Nov 15 '15 at 23:36

2 Answers2

2

I would refer you to the question that is answered here.

var audio = new Audio('audio_file.mp3');
audio.play();

That 's the basic of playing audio. Have a look at conditional later after the audio is playing from the link that Roko C. Buljan has given

Community
  • 1
  • 1
Liem Ly Quan
  • 61
  • 1
  • 5
0

you would like review how if, else if and else statements work.

if ('Hi.' + 'Hi!' + 'Hello.' + 'Hello!' + 'Greetings.' + 'Greetings!') {
    // The above statement will always evaluate to boolean `true`
    // Everything in here will always run and no `else`, `else if` will ever trigger

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else

To explore for additional errors, open Developer Tools in your browser (Preferably use Chrome) and see if any errors appear in Console ( like issues in getting the audio file etc...)

Using local file for Web Audio API in Javascript

Community
  • 1
  • 1
Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
  • Thank you for clarifying that because I had previously written this code with all separate "else-if" conditions but I had seen on another site like this to add plus signs to make the program shorter. This explains why it had been behaving oddly even before I put the audio in. – jc92me Nov 16 '15 at 00:14