Reputation: 367
This program prints out "Nobody is busy its just a matter of PRIORITIES". If the user answers "no" to the time question, I want it to say that if you answered "no" to the time question and yes to at least one other question. If you answer "no" to all the questions, it should instead say "You are not from us". I'm unsure how to alter this code to get this result.
var javascript = prompt("Want to learn javascript? (Type yes or no)");
var docker = prompt("Want to learn docker? (Type yes or no)");
var time = prompt ("do you have time ? (type yes or no)");
if(time === "no") {
alert("Nobody is busy its just a matter of PRIORITIES");
}
if ((javascript ==="yes" && time === "yes") && (docker === 'yes' && time ==='yes') ) {
alert("keep patience first learn docker and then learn javascript");
}
else if (javascript === "yes" && docker === "yes") {
if (time === "no") {
alert("so what should I do if u don't have time ?");
}
}
else if (javascript ==="yes" && time === "yes") {
alert("go and learn javascript");
}
else if (time ==='no' && javascript === "yes") {
alert("\"A smarter way to learn javascript\" will solve your problem in less time ");
}
else if (docker === 'yes' && time ==='yes') {
alert(' go n learn docker');
}
else if (time ==='no' && docker === "yes") {
alert("\"Docker Deep Dive\" will solve your problem in less time ");
}
else {
alert('You are not from us');
}
Upvotes: 2
Views: 786
Reputation: 367
All the recomendations and suggestions are really useful. I've changed the question. If you answer "no" to all the questions, it should instead say "You are not from us" and shouldn't print out "Nobody is busy its just a matter of PRIORITIES" in this case only else whenever time is "no" should prints "Nobody is busy its just a matter of PRIORITIES"
function promptToBoolean(txt) {
return /yes/i.test(prompt(txt));
}
var javascript = promptToBoolean("Want to learn javascript? (Type yes or no)");
var docker = promptToBoolean("Want to learn docker? (Type yes or no)");
var time = promptToBoolean("do you have time ? (type yes or no)");
if (!time) {
alert("Nobody is busy its just a matter of PRIORITIES");
}
if (javascript && time && docker) {
alert("keep patience first learn docker and then learn javascript");
} else if (javascript && docker && !time) {
alert("so what should I do if u don't have time ?");
} else if (javascript && time) {
alert("go and learn javascript");
} else if (!time && javascript) {
alert("\"A smarter way to learn javascript\" will solve your problem in less time ");
} else if (docker && time) {
alert('go n learn docker');
} else if (!time && docker) {
alert("\"Docker Deep Dive\" will solve your problem in less time ");
} else {
alert('You are not from us');
}
function promptToBoolean(txt) {
return /yes/i.test(prompt(txt));
}
var javascript = promptToBoolean("Want to learn javascript? (Type yes or no)");
var docker = promptToBoolean("Want to learn docker? (Type yes or no)");
var time = promptToBoolean("do you have time ? (type yes or no)");
if (time) {
if (javascript && docker) {
alert("keep patience first learn docker and then learn javascript");
} else if (javascript) {
alert("go and learn javascript");
} else if (docker) {
alert('go n learn docker');
}
} else {
alert("Nobody is busy its just a matter of PRIORITIES");
if (javascript && docker) {
alert("so what should I do if u don't have time ?");
} else if (javascript) {
alert("\"A smarter way to learn javascript\" will solve your problem in less time ");
} else if (docker) {
alert("\"Docker Deep Dive\" will solve your problem in less time ");
} else {
alert('You are not from us');
}
}
function promptToBoolean(txt) {
return /yes/i.test(prompt(txt));
}
var javascript = promptToBoolean("Want to learn javascript? (Type yes or no)");
var docker = promptToBoolean("Want to learn docker? (Type yes or no)");
var time = promptToBoolean("do you have time ? (type yes or no)");
if (time) {
if (javascript && docker) {
alert("keep patience first learn docker and then learn javascript");
} else if (javascript) {
alert("go and learn javascript");
} else if (docker) {
alert('go n learn docker');
}
} else {
alert("Nobody is busy its just a matter of PRIORITIES");
if (javascript && docker) {
alert("so what should I do if u don't have time ?");
} else if (javascript) {
alert("\"A smarter way to learn javascript\" will solve your problem in less time ");
} else if (docker) {
alert("\"Docker Deep Dive\" will solve your problem in less time ");
} else {
alert('You are not from us');
}
}
Upvotes: 4
Reputation: 33992
My first suggestion is to use true
/false
boolean instead of checking the strings "yes"
and "no"
to help simplify your code. I've made a funciton to help convert this for you. It also handles what happens if someone type "NO"
or "yEs"
for example.
Also, just using consistent formatting helps make the code easier to read.
function promptToBoolean(txt) {
return /yes/i.test(prompt(txt));
}
var javascript = promptToBoolean("Want to learn javascript? (Type yes or no)");
var docker = promptToBoolean("Want to learn docker? (Type yes or no)");
var time = promptToBoolean("do you have time ? (type yes or no)");
if (!time) {
alert("Nobody is busy its just a matter of PRIORITIES");
}
if (javascript && time && docker) {
alert("keep patience first learn docker and then learn javascript");
} else if (javascript && docker && !time) {
alert("so what should I do if u don't have time ?");
} else if (javascript && time) {
alert("go and learn javascript");
} else if (!time && javascript) {
alert("\"A smarter way to learn javascript\" will solve your problem in less time ");
} else if (docker && time) {
alert('go n learn docker');
} else if (!time && docker) {
alert("\"Docker Deep Dive\" will solve your problem in less time ");
} else {
alert('You are not from us');
}
Another way to help manage them is to better follow the logic, don't repeat yourself, and don't use unnecessary parenthesis or nested statements.
For example, this
if ((javascript ==="yes" && time === "yes") && (docker === 'yes' && time ==='yes') ) {
can be re-written as just
if (javascript && time && docker) {
And then this:
} else if (javascript === "yes" && docker === "yes") {
if (time === "no") {
alert("so what should I do if u don't have time ?");
}
}
can be re-written as:
} else if (javascript && docker && !time) {
alert("so what should I do if u don't have time ?");
}
I also recommend sectioning things off into large chunks to manage the logic, for example with time
, that seemed to be checked quite often, so you could just do that check once and then manage your other logic inside those blocks of code
if (time) {
//Put everything in here where time is true
} else {
//Put everything in here where time is false
}
Like this:
function promptToBoolean(txt) {
return /yes/i.test(prompt(txt));
}
var javascript = promptToBoolean("Want to learn javascript? (Type yes or no)");
var docker = promptToBoolean("Want to learn docker? (Type yes or no)");
var time = promptToBoolean("do you have time ? (type yes or no)");
if (time) {
if (javascript && docker) {
alert("keep patience first learn docker and then learn javascript");
} else if (javascript) {
alert("go and learn javascript");
} else if (docker) {
alert('go n learn docker');
}
} else {
alert("Nobody is busy its just a matter of PRIORITIES");
if (javascript && docker) {
alert("so what should I do if u don't have time ?");
} else if (javascript) {
alert("\"A smarter way to learn javascript\" will solve your problem in less time ");
} else if (docker) {
alert("\"Docker Deep Dive\" will solve your problem in less time ");
} else {
alert('You are not from us');
}
}
Upvotes: 4