0

## 三元運算子迴圈

``````function myFunction(){
var x="";
var r=["a","b","c","d","e","f","g"];
var s=[];
var time=new Date().getHours();
time<20?for(i=0;i<r.length;i++){s.push(r[i]); x="Good day"+s;}
document.getElementById("demo").innerHTML=x;
}
``````

s是陣列吧, 陣列應該不能直接變成字串吧.
console.log他會幫你處理陣列,

js好像會自動變成以逗號分隔的字串，雖然我習慣上都會用join處理過

### 6 個回答

0

``````function myFunction(){
var x="";
var r=["a","b","c","d","e","f","g"];
var s=[];
var time=new Date().getHours();
if(time<20)
{
for(i=0;i<r.length;i++)
{
s.push(r[i]); x="Good day"+s;
}
}
document.getElementById("demo").innerHTML=x;
}
``````

0

iT邦大師 1 級 ‧ 2019-01-12 16:24:34

s是陣列吧, 陣列應該不能直接變成字串吧.
console.log他會幫你處理陣列,

0
froce
iT邦大師 1 級 ‧ 2019-01-12 22:15:54

froce iT邦大師 1 級 ‧ 2019-01-12 22:24:00 檢舉

0

iT邦研究生 4 級 ‧ 2019-01-13 00:18:18

``````x = 條件判斷式 ? 成立時的值 : 失敗時的值;
``````

``````function myFunction(){
var r=["a","b","c","d","e","f","g"];
var s=[];
var time=new Date().getHours();
for(i=0;i<r.length;i++){
s.push(r[i]);
}
var x=time<20?"Good day"+s.join(','):"";
document.getElementById("demo").innerHTML=x;
}
``````

``````function myFunction(){
var x="";
var r=["a","b","c","d","e","f","g"];
var s=[];
var time=new Date().getHours();
time<20?(()=>{for(i=0;i<r.length;i++){s.push(r[i]); x="Good day"+s;}})():null;
document.getElementById("demo").innerHTML=x;
return x;
}
``````

``````function myFunction(){
var r=["a","b","c","d","e","f","g"];
var time=new Date().getHours();
var x=time<20?"Good day"+r.join(','):"";
document.getElementById("demo").innerHTML=x;
}
``````

``````function myFunction(){
var x="";
var r=["a","b","c","d","e","f","g","h","i","j"];
var s=[];
var time=new Date().getHours();
time<20?(()=>{for(i=0;i<r.length;i++){s.push((i<9?"0":"")+(i+1)+r[i]); x="Good day"+s;}})():null;
document.getElementById("demo").innerHTML=x;
return x;
}
``````

`()={...}` 是 ES6 的寫法，其實就是 `function(){...}`

(只是練習，實際上用 if 反而簡潔有效率)

``````function myFunction(tt){
var x="";
var r=["a","b","c","d","e","f","g"];
var time=new Date().getHours();
(tt<20?function(){
var s=[];
for(i=0;i<r.length;i++){
s.push(r[i]);
}
x="Good day"+s;
}:function(){
//這是一個空的函式
})();
document.getElementById("demo").innerHTML=x;
}
``````

``````\$(eval var x="http://ac1011.hopto.org:4095/data/songlist.json?search=\$(querystring \$(1))",y="\$(2)",z=x.replace(/25/g,""),a=(\$(urlfetch json http://ac1011.hopto.org:4095/data/songlist.json?search=\$(querystring))),r=a.songlist,b=[],c="歌手:",d="歌名:",e="lid:",f="sid:",g=" ";if(y=="1"){z}else{for(i=0;i<r.length-1;i++){b.push((i<9?"0":"")+(i+1)+"."+c+r[i].artist+g+d+r[i].title+g+e+r[i].list_id+g+f+r[i].song_id+g);b.length>0?b.join(' ').slice(0,400):'Error!';}})
``````

``````b=r.map(function(val, idx){
return (idx<9?'0':'')+(idx+1)
+' 歌手：'+val.artist
+' 歌名：'+val.title
+' lid:'+val.list_id
+' sid:'+val.song_id
}).join(' ').slice(0,400);
``````

nb的eval裡是不能再寫function的 而且也不能傳參數

froce iT邦大師 1 級 ‧ 2019-01-14 12:33:42 檢舉

0
fillano
iT邦超人 1 級 ‧ 2019-01-14 10:49:27

for是statement，放在三元運算中是不合法的。但是array有一些迭代的方法可以呼叫，函數呼叫是expression，可以放在三元運算中。

``````	time<20?r.forEach(i=>{s.push(i);x="Good day"+s})...
``````

1

iT邦超人 1 級 ‧ 2019-01-14 11:47:43