javascript 一問

25 回覆
1 Like 1 Dislike
2020-03-30 18:14:47
初學
唔識個loop 應該點寫
有冇高人指點一下


2020-03-30 18:19:23
forloop // do sth on x
{
 forloop // do sth on y
 {
  forloop // do sth on z
  {

  }
 }
}
2020-03-30 18:20:24
forloop // do sth on x
{
forloop // do sth on y
{
forloop // do sth on z
{
 if (x - 2y + 3z == 0)
then print result
}
}
}
2020-03-30 18:23:35
for (let x = 1; x <= 5; x++)
for (let y = 6; y <= 10; y++)
for (let z = 3; z <= 7; z++)
2020-03-30 18:36:52
我仲差啲咩 真係唔係好識啲詳細既code...


2020-03-30 18:40:53
js 有if then 架咩
2020-03-30 18:45:27
我唔識今晚要交
2020-03-30 18:51:03
重新學過啦咁你
google下for loop點用
2020-03-30 18:53:49
都唔止係for loop 問題
2020-03-30 18:57:29
中學功課黎?
2020-03-30 19:30:35
啱啱寫完,但唔想害左你。其實樓上已經俾左一半答案。下一半係呢度
講明先,其實我唔識javascript.

if ( (x-(2*y)+(3*z)) == 0){
console.log("x = " + x + ", y = " + y + ", z =" + z);
}
for loop syntax, https://www.w3schools.com/js/js_loop_for.asp

你可以用以下呢個寫。
https://codepen.io/pen/
2020-03-30 19:33:13
你已經害左佢
2020-03-30 19:36:20
你啱。
2020-03-30 22:10:45
[1, 2, 3, 4, 5].forEach(x => {
[6, 7, 8, 9, 10].forEach(y => {
[3, 4, 5, 6, 7].forEach(z => {
if (x - 2 * y + 3 * z == 0) {
console.log("x = " + x + ", y = " + y + ", z =" + z);
};
})
})
});
2020-03-31 13:14:01
重讀啦
錯撚哂
2020-03-31 13:32:26
 for x in range(1,6):
  for y in range(6,11):
    for z in range(3,8):
      if ( x - 2 * y + 3 * z  == 0 ):
        print("x={}, y={}, z={}".format(x,y,z))
2020-03-31 13:51:07
呢條問題可以變得好有趣,如果想optimize個time complexity嘅話 ,例如呢個case係有辦法唔洗寫三個loop,因為你係搵緊一個matrix嘅null space,loop完頭兩個variable,最後嗰個variable如果係within range嘅話,佢個value必然係unique,所以唔洗loop佢
2020-03-31 20:49:02
唔好水鳩人啦
咁簡單都話有趣
2020-03-31 21:11:41
唔係呀,而家講緊O(n^3)同O(n^2)嘅分別喎
2020-03-31 21:39:55
樓主企都未企到你就教人跑
2020-04-01 20:49:52
var x = [1, 2, 3, 4, 5];
var y = [6, 7, 8, 9, 10];

x.forEach(xNum => {
    y.forEach(yNum => {
        result = xNum + 2 * yNum;
        remainer = (xNum + 2 * yNum) % 3;
        if (!remainer) {
            console.log("x: " + xNum + " y: " + yNum + " z: " + result / 3);
        }
    })
})
吹水台自選台熱 門最 新手機台時事台政事台World體育台娛樂台動漫台Apps台遊戲台影視台講故台健康台感情台家庭台潮流台美容台上班台財經台房屋台飲食台旅遊台學術台校園台汽車台音樂台創意台硬件台電器台攝影台玩具台寵物台軟件台活動台電訊台直播台站務台黑 洞