我需要检查属性是否包含属性,现在我正在使用if示例中的语句
这是一个示例对象
var Foo = {
"bar" :
{
"foobar":
{
"barfoo":1
}
}
}
Run Code Online (Sandbox Code Playgroud)
我需要检查是否barfoo存在,但首先我需要检查是否所有其他属性都存在,因为它们可能不依赖于是否已经运行了另一个函数.我无法真正减少这种结构的水平,所以这不是一个选择.
目前我正在做类似的事情:
var d = Foo;
if (d) {
d = d.bar;
if (d) {
d = d.foobar;
if (d) {
d = d.barfoo;
if(d){
console.log(d);
}
}
}
}
Run Code Online (Sandbox Code Playgroud) 有没有办法让条件为if语句(x "not"<= 90)?或者是唯一的方法,(x <= 90)在if零件中使用,如果是真的则无所事事,然后执行需要做的任何事情else?我应该提到我在Java中这样做.
java if-statement conditional-operator conditional-statements
我用javascript制作了一个小型岩纸剪刀游戏.为了让游戏更好一点,我想做两件事.
1)如果玩家给出了除了摇滚,纸张或剪刀以外的答案,并提示"请在三个选项之一中选择:摇滚,纸张或剪刀"
我已经实现了类似的东西,但它只运行一次.我希望得到提示,直到给出三个答案中的一个
2)如果是平局,我想让游戏再次从顶部开始运行代码.我怎么能让程序再次从顶部开始?
这是代码
var userChoice = prompt("Do you choose rock, paper or scissors?");
if (userChoice === "rock")
{}
else if (userChoice === "paper"){}
else if (userChoice === "scissors"){}
else {
userChoice = prompt ("Please pick between one of the three options: rock, paper or scissors");
}
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
console.log( "The …Run Code Online (Sandbox Code Playgroud) String start_cd;
String end_cd;
int time_start_int;
int time_end_int;
opencon();
SqlCommand res = new SqlCommand("SELECT ID,Available,Type," + start_cd + "," +
end_cd + " FROM " + going +
" WHERE " + start_cd + "!=0 or " + end_cd + "!=0 and " +
time_start_int + " <= " + start_cd + " <= " + time_end_int + "", con);
SqlDataAdapter sda_res = new SqlDataAdapter(res);
DataTable dt_res = new DataTable();
sda_res.Fill(dt_res);
listBox1.DataSource=dt_res;
listBox1.DisplayMember="ID";
listBox2.DataSource = dt_res;
listBox2.DisplayMember = start_cd;
Run Code Online (Sandbox Code Playgroud)
我想获取sql表 …
switch(type)
{
case 'home':
console.log('home switch');
break;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码没有写入控制台,以下内容也没有:
switch(type)
{
case "home":
console.log('home switch');
break;
}
Run Code Online (Sandbox Code Playgroud)
但是,以下内容:
if (type == 'home')
{
console.log('home if');
}
Run Code Online (Sandbox Code Playgroud)
我不知道为什么.这不是一个显示阻止,我可以使用该if声明,但我真的很好奇为什么会这样.
注意:这些声明是直接替换,此处无需考虑.范围没有变化,没有代码我没有提到可能会干扰价值type.
javascript if-statement switch-statement conditional-statements string-evaluation
鉴于以下条件:
bool roundUp = (useSharps && lowerHalf) || (!useSharps && !lowerHalf);
//which means roundUp will be false if useSharps but !lowerHalf,
//or if !useSharps but lowerHalf
Run Code Online (Sandbox Code Playgroud)
有没有更短的方法来确定roundUp?
以下是可能的选项:
| useSharps | !useSharps |
|lowerHalf|!lowerHalf|lowerHalf|!lowerHalf|
-------------------------------------------
roundUp| V | X | X | V |
!roundUp| X | V | V | X |
-------------------------------------------
V = true, X = false
Run Code Online (Sandbox Code Playgroud)
更新:
也许我的代码错了.我正在寻找的最终结果是表中的内容.
是否可以根据其他条件使用不同的循环条件?
例如
boolean isDfa = true;
Run Code Online (Sandbox Code Playgroud)
如果isDfa是true,我希望while循环的条件是:
while(!s.hasAllTransitions())
Run Code Online (Sandbox Code Playgroud)
否则,如果isDfa是false,我希望while循环的条件是:
while(!input.equals("next"))
Run Code Online (Sandbox Code Playgroud)
使用两个独立的循环是实现这一目标的唯一方法吗?
我想创建一个"冒泡排序"方法,这意味着我在一个数组中取两个连续元素,比较它们,如果左边元素大于右边元素,它们应该切换位置.我想重复它,直到我的数组按升序排序.
我的代码只能部分工作.如果我的数组太大,就不会发生任何事情(我必须用CTRL + C退出ruby).对于小于5个元素的数组,我的代码工作正常:
def bubbles(array)
while array.each_cons(2).any? { |a, b| (a <=> b) >= 0 }
# "true" if there are two consecutives elements where the first one
# is greater than the second one. I know the error must be here somehow.
array.each_with_index.map do | number, index |
if array[index + 1].nil?
number
break
elsif number > array[index + 1]
array[index], array[index + 1] = array[index + 1], array[index] # Swap position!
else
number
end
end
end
p …Run Code Online (Sandbox Code Playgroud) 为什么这样(Firebug控制台):
> "?" == ";"
> false
Run Code Online (Sandbox Code Playgroud)
但是这个:
> ';' == ';'
> true
Run Code Online (Sandbox Code Playgroud)
谁知道为什么会这样?
我有两张桌子,
表用户:id,name等
表视频:id,user_id等
型号:视频
public function user()
{
return $this->belongsTo('App\Models\User');
}
Run Code Online (Sandbox Code Playgroud)
我想选择用户"Max"所拥有的所有视频.
我尝试过以下内容:
$Videos = \App\Models\Video::with('user')->where('user.name','=','Max')->get();
Unknown column 'user.name' in 'where clause' (SQL: select * from `videos` where `videos`.`deleted_at` is null and `user`.`name` = Max)
Run Code Online (Sandbox Code Playgroud)
谢谢!
javascript ×4
c# ×2
if-statement ×2
java ×2
compare ×1
eloquent ×1
equality ×1
equals ×1
firebug ×1
laravel ×1
properties ×1
relationship ×1
ruby ×1
sql ×1
sql-server ×1
syntax ×1