JustA13YearOld
JustA13YearOld

Reputation: 19

Random Song Loop won't work with a function

I am new to JavaScript and I have an issue. When I try to run the code below within a function, nothing will show up in the console. I've successfully tried it without the function and it shows the proper strings in the console. Is there anyway I can use a function with this code or is there some other adjustments I need to make?

JavaScript:

function randSong() {
var i = Math.floor(Math.random()*10);
var listOfSongs = ['Killomanjaro','No Sad No Bad','Doomsday','Solitaire',
'Distance','Roll In Peace','Bank Account','SAD!','Moonlight','Swimming Pools'];
console.log('Alexa, play',listOfSongs[i]);
}

Here's the repl.it:https://repl.it/@OoferGangx7/random-song-loop

EDIT: I made a really dumb mistake, I forgot to call the function but case closed.

Upvotes: 1

Views: 36

Answers (2)

Carlos Alves Jorge
Carlos Alves Jorge

Reputation: 1985

Pretty sure you are not calling the function, no?

    function Play(){
    var i = Math.floor(Math.random()*10);
    var listOfSongs = ['Killomanjaro','No Sad No Bad','Doomsday','Solitaire',
    'Distance','Roll In Peace','Bank Account','SAD!','Moonlight','Swimming Pools'];
    console.log('Alexa, play',listOfSongs[i]);
    }

//you need to call it
    Play();

Upvotes: 0

Anis R.
Anis R.

Reputation: 6912

Well, I wrapped your code inside a function, and it seems to be working fine:

function playRandomSong() {
  var i = Math.floor(Math.random()*10);
  var listOfSongs = ['Killomanjaro','No Sad No Bad','Doomsday','Solitaire', 'Distance','Roll In Peace','Bank Account','SAD!','Moonlight','Swimming Pools'];
  console.log('Alexa, play',listOfSongs[i]);
}

playRandomSong();

Upvotes: 1

Related Questions