Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Keith Greatz
4,377 PointsPrompt always creates a string value
Hi, I am having some issues with values other than string, basically everything is being recognised as a string.
I know there are ways to be able to change my code slightly that will allow it to work but ultimately I’m wanting to find the cause of the issue. When I run the below script the user is prompted for a response. it appears that the response is automatically being converted into a string leaving the response to always be
" Sorry Thats Wrong! The answer is “+questionarray [ i ] [ 1 ]
is there a way to make prompt get the correct value’s for null, true, false, and numbers?
I'm using Chrome as a browser, it only seems to return the value null on a prompt when the cancel button is clicked.
var questionarray = [
["Europeans were the first people that lived in the Townsville region",false,"The first people who lived in the Townsville region were the Bindal and Wulgurukaba People. The Bindal knew the area as Thul Garrie Waja and the Wulgurukaba People knew it as Gurrumbilbarra." ],
["In which year was a site for the new settlement \( latter to be known as Townsville \) founded.", 1984,1984 ],
["The city Townsville named after Andrew Towns?",false,"The City of Townsville is named after Robert Towns although he never actually visited the city until years after it was established."],
];
function printh1 (message)
{ document.write ("<h1>" +message+ "</h1>");
}
function print (message)
{ document.write (message);
}
for ( var i = 0; i < questionarray.length; i += 1 ) {
printh1 (questionarray[i][0]);
var answer = prompt("Type Your Answer Here");
console.log(answer);
print(answer);
if (answer === null ) {
print("If you type nothing your answer will be considered wrong")
}
if (answer === questionarray[i][1]) {
print(" Thats Correct! The answer was "+questionarray[i][1]);
}
if (answer !== questionarray[i][1]) {
print(" Sorry Thats Wrong! The answer is "+questionarray[i][1])
}
console.log(answer);
console.log (i);
}
Thanks for any help! Keiffy101
2 Answers
Jacob Mishkin
23,118 PointsI think it's always good to keep the idea of scope of the project in mind. What I mean by that is; do you really need a Boolean value for your answers? is there anything in the code or will be in the code that will require a Boolean value? what is the difference between the string false and the Boolean value of false for your code? For numbers you need to use the parseInt() method, that's a given, and required in your quiz game, but if something doesn't work or there a simpler way of coming to the answer and having the program function, I would say go with that.
Jacob Mishkin
23,118 PointsI would have to do more research regarding boolean values, but you need to use the parseInt() method to convert input from the prompt method and change the values from a string to a number. Any input that is entered from the prompt method is returned as a string.
so like so:
var answer = prompt("what is your favorite number?");
if (parseInt(answer) === 3){
alert("good choice");
}
Keith Greatz
4,377 PointsYea, maybe i'm expecting to much lol, I can see all returned values are stings, I did find a function someone created to convert strings to booleans but it does it for the number questions also, I think i just need a different approach for the answers.
in case you were interested
stringToBoolean: function(string){
switch(string.toLowerCase()){
case "true": case "yes": case "1": return true;
case "false": case "no": case "0": case null: return false;
default: return Boolean(string);
}
}
Keith Greatz
4,377 PointsKeith Greatz
4,377 PointsThanks, the truth is I dont need it to be boolean, a string will suit the purpose just fine. it is good to know exactly how flexible the code is though, I have wasted a fair bit of time trying to understand why my === operator and !== operators would not function how I would like and the whole time it has been the value type that has been the issue.