Need some help plz

var userChoice = prompt(“how are you feeling today on a scale from 1 to 10?”);
if (userChoice == 1) {
console.log (“line1”);
console.log (“line2”);
console.log (“line3”);
}

if the user inputs 1 how can i make the console log print 1 of the 3 lines randomly?
could some plz give me an example of how you would code this?

We could start with a preset random number 0, 1, or 2.

var choice = Math.floor(Math.random() * 3)

if (...) {
    console.log(['line1', 'line2', 'line3'][choice])
}

how would i input this in my code?

Show us what you have and we can sort this out.

var userChoice = prompt(“how are you feeling today on a scale from 1 to 10?”);
var computerChoice = Math.random();
if (userChoice == 1) {
if (computerChoice <= 0.33){
computerChoice = “aww that sucks! hope you feel better soon”;
}
else if(computerChoice >= 0.34 && 0.66){
computerChoice = “thats not nice to hear :frowning: hope you will cheer up soon!”;
}
else{
computerChoice = “thats afwull! hope you have a nice day anyway!”;
}
}

var computerChoice = Math.random();
if (userChoice == 2) {
if (computerChoice <= 0.33){
computerChoice = “ooh thats not good! heres a smiley for you! :)”;
}
else if(computerChoice >= 0.34 && 0.66){
computerChoice = “to bad! cheer up it will get better!”;
}
else{
computerChoice = “aww that terrible :frowning: thinks will get beter soon!”;
}
}

var computerChoice = Math.random();
if (userChoice == 3) {
if (computerChoice <= 0.33){
computerChoice = “just hang in there things will get better”;
}
else if(computerChoice >= 0.34 && 0.66){
computerChoice = “im here for you :slight_smile: to cheer you up with a smiley :)”;
}
else{
computerChoice = “aww you can do better! cheer up”;
}
}

var computerChoice = Math.random();
if (userChoice == 4) {
if (computerChoice <= 0.33){
computerChoice = “thats not so good. but hey thinks will get better soon :)”;
}
else if(computerChoice >= 0.34 && 0.66){
computerChoice = “hmm thats not so nice to hear, cheer up champ!”;
}
else{
computerChoice = “there are allot off people out there! your not alone”;
}
}

var computerChoice = Math.random();
if (userChoice == 5) {
if (computerChoice <= 0.33){
computerChoice = “mwhaa could be better right ;)”;
}
else if(computerChoice >= 0.34 && 0.66){
computerChoice = “comon cheer up mate! your not alone!”;
}
else{
computerChoice = “hang in there champ! thinks will get better”;
}
}

var computerChoice = Math.random();
if (userChoice == 6) {
if (computerChoice <= 0.33){
computerChoice = “me too… me too…”;
}
else if(computerChoice >= 0.34 && 0.66){
computerChoice = “hey! thats not so bad, your day might get even better!”;
}
else{
computerChoice = “hey thats not bad at all there are people out there doing worse!”;
}
}

var computerChoice = Math.random();
if (userChoice == 7) {
if (computerChoice <= 0.33){
computerChoice = “nice! keep up the good vibes”;
}
else if(computerChoice >= 0.34 && 0.66){
computerChoice = “hey you! you rock!”;
}
else{
computerChoice = “keep it up! you might cheer up someone else with your good atitude :)”;
}
}

var computerChoice = Math.random();
if (userChoice == 8) {
if (computerChoice <= 0.33){
computerChoice = “marvelous! keep it up!”;
}
else if(computerChoice >= 0.34 && 0.66){
computerChoice = “thats so great to hear!”;
}
else{
computerChoice = “ooh wauw! thats great to hear!”;
}
}

var computerChoice = Math.random();
if (userChoice == 9) {
if (computerChoice <= 0.33){
computerChoice = “yeaah! great to see ur happy!”;
}
else if(computerChoice >= 0.34 && 0.66){
computerChoice = “wonderfull! keep up the good vibes!”;
}
else{
computerChoice = “hey there! did you know?.. you are awsome!”;
}
}

var computerChoice = Math.random();
if (userChoice == 10) {
if (computerChoice <= 0.33){
computerChoice = “did you just win the lottery??”;
}
else if(computerChoice >= 0.34 && 0.66){
computerChoice = “amazing! i hope you keep feeling this way!”;
}
else{
computerChoice = “great! share your good vibes!”;
}
}

Start with this line,

var computerChoice = Math.floor(Math.random() * 3)

You only need to run this line once.

Since there are so many cases, a switch might be easier to organize:

var function cheer(user, computer) {
    switch (user) {
    case 1: return [
        "Aw, that sucks! Hope you feel better soon.",
        "That's not nice to hear. Hope you will cheer up soon!",
        "That's awful! Hope you have a nice day anyway!"
        ][computer];
    case 2: return [
    
        ][computer];
    case 3: return [
    
        ][computer];
    case 4: return [
    
        ][computer];
    case 5: return [
    
        ][computer];
    case 6: return [
    
        ][computer];
    case 7: return [
    
        ][computer];
    case 8: return [
    
        ][computer];
    case 9: return [
    
        ][computer];
    case 10: return [
    
        ][computer];
    default: return "The input is not in range."
    }
};

console.log(cheer(userChoice, computerChoice));

Add three strings to each of the remaining case arrays and fix up the grammar for a more professional finish and you’re done.

1 Like

thank you all for your help! i worked it out and it works! :slight_smile:
really nice and helpful comunity here !

1 Like