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

JavaScript JavaScript Basics (Retired) Making Decisions with Conditional Statements Introducing Conditional Statements

what is window.alert? I am unable to print an alert dialog box with 'alert' and i dont know why.

It is from a quiz in javascript basics

app.js
var answer = prompt("What is the best programming language?");
if(answer.toUpperCase === 'JavaScript')
{
alert("You are correct");
} else
{
alert("You are sooo right!");
}
index.html
<!DOCTYPE HTML>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>JavaScript Basics</title>
</head>
<body>
<script src="app.js"></script>
</body>
</html>

1 Answer

Sam Gord
Sam Gord
14,084 Points

Hi, window is an object that represents an open window in a browser. and alert() is a function ( or method ) which belongs to window object, and it displays an alert box with a message and an OK button. u can use it like

window.alert('"You are sooo right!");

or simply write it like this

alert('"You are sooo right!");

but there is also something else i want to mention about your code , i think the result of the if statement is always going to be the else block : You are sooo right! because of two reasons , 1st- toUpperCase is a function and it must be fired to have a result and that result then will be compared to the condition we check in the if block which is 'JavaScript'. u can check what answer.toUpperCase() is look like by logging it in the console

console.log(answer.toUpperCase);

so it has to be written this way i think

var answer = prompt("What is the best programming language?");
if(answer.toUpperCase() === 'JavaScript')
{
alert("You are correct");
} else
{
alert("You are sooo right!");
}

2nd- the result of toUpperCase() method is always an uppercase string. for example, no matter what the user types in the prompt input, the result will always be something in all caps, like this ---> "JAVASCRIPT" and not like the main condition . so again there will be no result as "You are correct".

hope it helped, Happy New Year and Happy Coding :D