hi guys there i a project in javascript course called (Magic Eight Ball) i done it and i even did some upgrading to the code but something drive me crazy
here is the code = (i say my problem after the code )
//username
let userName = "";
//user Question
const userQuestion = "";
//chech if user have a name if yes reapet there name
if ((userName = userName)) {
console.log(`${userName}`);
//if not don't repeat
} else {
console.log("");
}
//check if user wrote the name or not if no tell them to do so
if (userName === "" && userQuestion === "") {
console.log("write your name");
//if there was no question ask them to wrote it down
} else if (userQuestion === "") {
console.log(`ask a question`);
// if there was a name reapet the question
} else {
console.log("");
}
//making the random numbers for the ball
const randomNumber = Math.floor(Math.random() * 8);
//make the ball
let eightBall = "";
//connect the ball to the numbers
eightBall = randomNumber;
// the answers to each random number that generate
let answers = "";
switch (eightBall) {
case 0:
eightBall = `${userName} can you ask one more time`;
break;
case 1:
eightBall = "so do it";
break;
case 2:
eightBall = `${userName} try again`;
break;
case 3:
eightBall = "Cannot predict now";
break;
case 4:
eightBall = "My sources say no";
break;
case 5:
eightBall = "Outlook not so good";
break;
case 6:
eightBall = "Signs point to yes";
break;
case 7:
eightBall = "try your best";
break;
case 8:
eightBall = "do it any way";
break;
}
//now if the user didnt ask a question we say nothing
if (userQuestion === "") {
console.log("");
//but now if user asked a question but didnt wrote the name we tell them to write down the name too
} else if (userName === "") {
console.log(
"you asked a question but write down your name so i can tell you the answer"
);
}
// now if user wrote a name and ask a question we give them the answer
else {
console.log(`the answer to (${userQuestion}) is = ${eightBall}`);
}
ok the code run perfectly but when there is no name the output is =
(
write your name
)
with exactly a space above it
i have that little space above it i tried using the (.trim , .trimEnd)
but didn’t work
does anyone have a solutiion ?
At the beginning you have if ((userName = userName)) {
but if you want that to run only if userName is an empty string, then, instead, you could do if (userName !== "") {
or if (userName) { since non-empty strings are truthy (treated like true for the if-statement conditions)
Also, the
} else {
console.log("");
}
prints in a line with nothing on it to the console.
If you don’t want an empty line, you could leave that out.
Also, if its possible that username is blank, you might do something like eightBall = `${userName} try again`.trim();
in the relevant cases.
//username
let userName = "";
//user Question
const userQuestion = "";
//chech if user have a name if yes reapet there name
if (userName !== "") {
console.log(`${userName}`);
//if not don't repeat
} else {
}
//check if user wrote the name or not if no tell them to do so
if (userName === "" && userQuestion === "") {
console.log("write your name");
//if there was no question ask them to wrote it down
} else if (userQuestion === "") {
console.log(`ask a question`);
// if there was a name reapet the question
} else {
}
//making the random numbers for the ball
const randomNumber = Math.floor(Math.random() * 8);
//make the ball
let eightBall = "";
//connect the ball to the numbers
eightBall = randomNumber;
// the answers to each random number that generate
let answers = "";
switch (eightBall) {
case 0:
eightBall = `${userName} can you ask one more time`.trim();
break;
case 1:
eightBall = "so do it";
break;
case 2:
eightBall = `${userName} try again`;
break;
case 3:
eightBall = "Cannot predict now";
break;
case 4:
eightBall = "My sources say no";
break;
case 5:
eightBall = "Outlook not so good";
break;
case 6:
eightBall = "Signs point to yes";
break;
case 7:
eightBall = "try your best";
break;
case 8:
eightBall = "do it any way";
break;
}
//now if the user didnt ask a question we say nothing
if (userQuestion === "") {
console.log("");
//but now if user asked a question but didnt wrote the name we tell them to write down the name too
} else if (userName === "") {
console.log(
"you asked a question but write down your name so i can tell you the answer"
);
}
// now if user wrote a name and ask a question we give them the answer
else {
console.log(`the answer to (${userQuestion}) is = ${eightBall}`);
}