Eightball challenge - project

Hello everybody! i’m currently extra confused about something, so i have this code let userName = ‘’;
userName ? console.log(‘Hello’) : console.log(‘Hello!’);

const userQuestion = ‘Am i becoming a vampire?’;
console.log(The user asked: ${userQuestion});

const randomNumber = Math.floor(Math.random() * 8);

let eightBall = ‘’;

switch (randomNumber){
case 0:
eightBall = ‘It is certain’;
break;
case 1:
eightBall = ‘It is decidedly so’;
break;
case 2:
eightBall = ‘Reply hazy try again’;
break;
case 3:
eightBall = ‘Cannot predict now’;
break;
case 4:
eightBall = ‘Do not count on it’;
break;
case 5:
eightBall = ‘My sources say no’;
break;
case 6:
eightBall = ‘Outlook not so good’;
break;
case 7:
eightBall = ‘Signs point to yes’;
break;
}

console.log(The eight ball answered: ${eightBall});

sorry it might seem spammy but i’m not sure if i can upload .js files and actually get people to read it… so anyways, i have a question regarding the use of the let userName = ’ '; so i have that string blank, i want that element doing exactly what the supposed headline says i want that to return that if somebody inputs a name it gets autofilled to my operary ternator : for example if the blank of userName get’s filled i want that to reflect in userName ? console.log(“true”) : console.log(“false”);

I’m not sure if i’m explaining myself correctly but i guess ill try it this way, i know this problem is probably something really easy to solve but right now i feel like i don’t really get it, i mean it works right now but i want that additional perk, it’s like some kind of user input that get’s autofilled with an automated answer using the “input” part

1 Like

Hello @snox22! Welcome to the forum!
There really isn’t a way to get actual user input from the console on the Codecademy site using JavaScript. JavaScript is primarily used in conjunction with HTML and CSS, so there isn’t a method like you see in other languages like Ruby, Python, C++, C# for getting user input from the console. All you can do with this project is type the name in when you make the assignment: let userName = 'Sheldon Cooper'; for example. Then you can use the same string interpolation syntax that you used to console.log() the user’s question to insert userName into your output in your ternary statement. Hope this helps!

2 Likes

i think you can use prompt:

https://developer.mozilla.org/en-US/docs/Web/API/Window/prompt

its quite an annoying popup, but it works within the codecademy environment.

1 Like

I’ve tried it, and always get an error. ReferenceError: prompt is not defined at Object.<anonymous> (/home/ccuser/workspace/learn-javascript-U2P1/main.js:2:16)
Just tried it again in the Magic Eightball lesson. Same error. Works in Google console, but not Codecademy. Unless there’s some special trick to it?

1 Like

Oh, i thought that worked. Long time ago it used, no longer then i suppose

then i recommend the usage jsbin/jsfiddle to run your code where you do have html and/or can use prompt.

2 Likes

Hey mid! thanks for the help! Yeah i was kinda stuck there for a long time thinking what was i supposed to do to get autofilled responses from a name.

I did it the way you especified here i used the let userName = ‘Greg’ - be filled with something instead of blank. and then using the ternary operator of userName ? console.log(‘Hello Greg’) : console.log(‘Hello!’); in case the userName ever get’s blank.

But i’m pretty sure i read somewhere you can use templates to autofill something like that. Like if i leave it blank and i somehow put something there later the ““TRUE”” value of my ternary get’s triggered with the info anyway like if in case i don’t have a defined name in userName - it goes something like this >

let userName = ‘’ (blank right now but i will put a predetermined name here
userName ? console.log(‘Hello (the name i will input in a later date here)’) : console.log(‘Hello (name still blank)’); – i guess it does make more sense like this right?

1 Like

I’m not sure I’m following you exactly, but you could do something like this:

let userName = '';

const greeting = name => {
  console.log( name ? `Hello ${name}!` : 'Hello unidentified carbon based life form!');
  //another more concise way to do this:
  //console.log(`Hello ${name? name : 'unidentified carbon based life form'}!`);
}

greeting(userName);

userName = 'Steve';

greeting(userName);

Output:

Hello unidentified carbon based life form!
Hello Steve!

There are any number of ways to implement the logic you are describing. You’ll learn more of them as you progress through the lessons. This method, for example, uses a default parameter, and eliminates the need for the ternary expression:

const greeting = (name = 'unidentified carbon based life form') => {
  console.log(`Hello ${name}!`);
}

let userName = undefined;

greeting(userName); //since userName is 'undefined' the default will be assigned

userName = 'Steve'; 

greeting(userName); //since userName is now assigned 'Steve' the default value will not be assigned

Output:

Hello unidentified carbon based life form!
Hello Steve!

Keep up the good work! Looking for ways to expand on these exercises is a great way to learn :+1:

1 Like

Node io isn’t as straight-forward as, say, C or Python where you can stop the program while waiting for user input.

But with promises you can get something that looks similar:
(disclaimer, I have 23% of a clue what I’m doing here)

const readline = require('readline')
const rl = readline.createInterface(process.stdin, process.stdout)

const input = (prompt) => {
  return new Promise((resolve, reject) => {
    rl.question(prompt, answer => {
      resolve(answer)
    })
  })
}

const main = async () => {
  const name = await input('Who are you? ')
  console.log(`Hello ${name}`)
  const color = await input('What is your favourite color? ')
  console.log(`What is wrong with you, ${color} is the worst!`)
  rl.close()
}

main()

Oh and if codecademy is using some ubuntu image with 5 year old packages then this probably doesn’t run there. Maybe if you feed the code into bable first. Dunno. It could also be rewritten as nested callbacks but I doubt anyone actually likes that.

Maybe someone knows how to flatten this (without async):
(but if somehow flattening it then the previous name bindings would get lost so uh… ?? Anyway I’m sure people have some way of dealing with it but I don’t write any javascript)

const main = () => {
  input('Who are you? ').then(name => {
    console.log(`Hello ${name}`)
    input('What is your favourite color? ').then(color => {
      console.log(`What is wrong with you, ${color} is the worst!`)
      rl.close()
    })
  })
}

…I managed to flatten it, and passed an object around to store variables in.
But now it’s really difficult to figure out what’s going on and in what order.

const askName = () => {
  return new Promise((resolve, reject) => {
    input('Who are you? ').then(name => {
      console.log(`Hello ${name}`)
      resolve({ name: name })
    })
  })
}

const askColor = (scope) => {
  return new Promise((resolve, reject) => {
    input('What is your favourite color? ').then(color => {
      console.log(`What is wrong with you, ${color} is the worst!`)
      console.log(`Earlier you told me that your name is ${scope.name}`)
      resolve()
    })
  })
}

const main = () => {
  askName()
    .then(askColor)
    .then(() => rl.close())
}

main()

Bottom line: jonatan doesn’t know any js, async is hard, and async/await keywords help a whole lot.

1 Like

Thanks a lot mid! this is exactly what i was looking for, yeah i know i didn’t explain myself that well haha i just didn’t precisely understand the correct way to express what i really wanted to convey this time, but you still got it anyways i just had to come back to it once more and see it one more time to kinda make it more understandable haha, anyways thanks for all the help!

2 Likes

Oh wow i didn’t really know you could do it like that, seems like a lot of code could be more simplified like in the first one you did, but after you convert it to javascript it’s more like trying to write something that becomes more complicated than it is haha, thanks a lot for that information though!

1 Like

The first one is javascript.

But I suppose you were really only asking about string formatting. I saw talk of prompt … so I went off about how to read user input from stdin

1 Like