Console outputs into an array

I am running a code that loops 7 times and I have made it to console.log the user input for every loop. I want to put each console.log output into an array. how do I go about doing this?

the following is my output

Debugger attached.
The Itsy Bitsy Aardvark

Please choose an animal name :
a)aardvark
b)hippo
c)duck
d)pelican
e)t-rex
Enter choice (a-e): a
[ ‘aardvark’ ] - output 1
Please choose an action word ending in ‘ed’ :
a)jumped
b)flipped
c)looked
d)added
e)squashed
Enter choice (a-e): a
[ ‘jumped’ ] - output 2
Please choose an adjective :
a)impressive
b)slimy
c)silly
d)frozen
e)magical
Enter choice (a-e): a
[ ‘impressive’ ] - output 3
Please choose another action word ending in ‘ed’ :
a)exploded
b)scattered
c)flapped
d)snooped
e)poked
Enter choice (a-e): a
[ ‘exploded’ ] - output 4
Please choose a noun :
a)president
b)pastry chef
c)karate
d)beer mug
e)tank
Enter choice (a-e): a
[ ‘president’ ] - output 5
Please choose another action word ending in ‘ed’ :
a)messed
b)smacked
c)tripped
d)danced
e)slurped
Enter choice (a-e): a
[ ‘messed’ ] - output 6
Please choose an adverb :
a)dreadfully
b)happily
c)stupidly
d)awkwardly
e)beautifully
Enter choice (a-e): a
[ ‘dreadfully’ ] - output 7

I want to be able to get all the outputs 1-7 into an array. how do i go about this? Thank you in anticipation.

I don’t understand what you are asking. Why put the “console.log output” into an array? Why not put the selection into an array?

If you have further questions, seeing the code might help.

1 Like

Thank you. I will put up the code (i am relatively new to this and still struggling with coding)

const rls = require("readline-sync");

const fs = require('fs');

console.log("The Itsy Bitsy Aardvark \n\n");

const storycontents=fs.readFileSync("the_story_file.txt","utf8");

const arrStory = storycontents.split("\r\n");

const csvcontents=fs.readFileSync("the_choices_file.csv","utf8");

const arrChoices = csvcontents.split("\r\n");

const arrLetters = ['a)','b)','c)','d)','e)'];

//For loop repeating the code for 7 times

for(let j=0;j<arrChoices.length;j++)

{

   

    const arrCols = arrChoices[j].split(',');

console.log("Please choose", arrCols[0],':');

//this removes the first line of each arrayCols

let choices = arrCols.shift();

// For loop executing the main code

arrCols.forEach(function(index,i)

{

    console.log(arrLetters[i]+index);

})

userInput = rls.question("Enter choice (a-e): ");

//saveInput for storing answers

saveInput="";

saveInput=[userInput];

// console.log(saveInput);

//if statement for checking correct userInput

if (userInput == "a")

{

    saveInput[0] = arrCols[0];

} else if (userInput == "b")

{

    saveInput[0] = arrCols[1];

} else if (userInput == "c")

{

    saveInput[0] = arrCols[2];

} else if (userInput == "d")

{

    saveInput[0] = arrCols[3];

} else if (userInput == "e")

{

    saveInput[0] = arrCols[4];

} else rls.question("Enter choice (a-e): ");

    console.log(saveInput);

}

by the way, the choices were pulled from a text file. i hope just looking at the code can help

When you print the value you can also store it into an array, just add a line near the console log that does that with the value printed by the console log

1 Like

Thank you. i worked through the problem and got the solution i was looking for.