这个简单的JavaScript代码意味着什么 [英] What does this simple javascript code means

查看:88
本文介绍了这个简单的JavaScript代码意味着什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  switch (提示(  Whatistheweatherlike?))


{
case rainy
console .log(< span class =code-string> Remembertobringanumbrella。);
break ;
case sunny
console .log( Dresslightly。 );
case 多云
console .log( Gooutside。 );
break ;
默认
console .log( Unknownweathertype!);
break ;
}





我的问题是第一行是如何工作的?



开关(提示(Whatistheweatherlike?))



基本上,我们正在比较用户输入和打印消息。



通常,我们使用prompt(message,);存储变量。我的问题是为什么我们在任何变量中存储存储值?

解决方案

switch语句基本上是一个if语句,不重复这个问题。



上面你的功能相当于:



  var  response =  switch (提示(  Whatistheweatherlike))?; 

if (response == rainy){
console 。 log( Remembertobringanumbrella。);
}
else if (response == sunny){
console .log( Dresslightly。);
}
else if (response == 多云){
console .log( Gooutside。);
}
else {
console .log( Unknownweathertype!);
}


好吧,

这很简单。

当你运行这个并且开关被击中,它会提示用户输入天气是什么样的?

javascript中的窗口提示符()意味着窗口中的所有内容都会被提示对话框或对话框覆盖,提示用户根据问题输入确定和取消按钮,单击确定。 ,检查Case语句并记录输出消息。

根据用户输入的情况进行检查并在控制台日志中打印输出消息。

小提琴 [ ^ ]

请检查小提琴。

我希望这会有所帮助。

谢谢。:)

switch(prompt("Whatistheweatherlike?"))


{
case"rainy":
console.log("Remembertobringanumbrella.");
break;
case"sunny":
console.log("Dresslightly.");
case"cloudy":
console.log("Gooutside.");
break;
default:
console.log("Unknownweathertype!");
break;
}



My question is how the first line works?

switch(prompt("Whatistheweatherlike?"))

Basically, we are comparing user input and printing a message.

Usually, we use prompt("message",""); to store a variable. My question is why we havnt stored value in any variable?

解决方案

A switch statement is basically an if statement, that doesn't repeat the question.

What you have above is the functional equivalent of:

var response = switch(prompt("Whatistheweatherlike?"));

if(response == rainy) {
console.log("Remembertobringanumbrella.");
}
else if(response == "sunny") {
console.log("Dresslightly.");
}
else if(response == "cloudy") {
console.log("Gooutside.");
}
else {
console.log("Unknownweathertype!");
}


Well,
this is quite simple.
When you run this and switch gets hit, it prompts the user to input What the weather is like?
Window Prompt() in javascript means that everything in the window gets overboard by the prompt dialog or dialog, which asks the user to input based on the questions with a Ok and Cancel button, On click of Ok, the Case statements gets checked and the output message gets logged.
And based on the input by the user case is checked and output message is printed in the console log.
Fiddle[^]
Please check the fiddle also.
I hope this helps.
Thanks.:)


这篇关于这个简单的JavaScript代码意味着什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆