生成器总是在随机句子生成器中返回相同的故障事件

时间:2020-08-30 18:56:51

标签: javascript

它总是很奇怪地返回一个不是我想要的句子。这是代码

function erase(){document.open()}
alert("welcome to random sentence genereator and your sentence is on the screenand in the console below")
var b =Math.floor(Math.random() * 5);
if (b="1"){var a=("i ")}
else if(b="2"){var a=("a bean ")}
else if(b="3"){var a =("a xbox ")}
else if(b="0"){var a =("sport drink ")}
else if (b="4"){var a =("some grass ")}
else if(b="5"){var a =("a rock ")}
var e=Math.floor(Math.random() * 4);
if(e="0"){var p =("fell on ")}
else if(e="1"){var p =("jumped on ")}
else if(e="2"){var p =("bounced over ")}
else if(e="3"){var p =("slid on")}
else if(e="4"){var p =("slipped on")}
var n=Math.floor(Math.random() * 6);
if(n="0"){var q=("a mountain ")}
if(n="1"){var q =("a candlestick ")}
else if(n="2"){var q =("a raw potato ")}
else if(n="3"){var q=("a book ")}
else if(n="4"){var q=("a glass bottle ")}
else if(n="5"){var q =("water ")}
else if(n="6"){var q =("fish soup ")}
var o=Math.floor(Math.random() * 3);
if(o="0"){var v =("in the dark")}
if (o="1"){var v =("in the light")}
if(o="2"){var v=("in the middle of nowhere")}
if(o="3"){var v=("in a giant lemon")}
document.write(a+p+q+v)

为什么不起作用并且总是奇怪地返回一个句子。 plz遮阳篷

2 个答案:

答案 0 :(得分:1)

b是一个数字,因此应将其与b == '3'b === 3进行比较。 另外,Math.floor(Math.random() * 5)会生成一个介于0到4之间的数字(并且永远不会是5),因此您将永远不会进入b == 5

答案 1 :(得分:1)

尝试输入以下代码

function erase() {
    document.open()
}
alert("welcome to random sentence genereator and your sentence is on the screen and in the console below")
var b = Math.floor(Math.random() * 5);
if (b === 1) {
    var a = ("i ")
} else if (b === 2) {
    var a = ("a bean ")
} else if (b === 3) {
    var a = ("a xbox ")
} else if (b === "0") {
    var a = ("sport drink ")
} else if (b === 4) {
    var a = ("some grass ")
} else if (b === 5) {
    var a = ("a rock ")
}
var e = Math.floor(Math.random() * 4);
if (e === 0) {
    var p = ("fell on ")
} else if (e === 1) {
    var p = ("jumped on ")
} else if (e === 2) {
    var p = ("bounced over ")
} else if (e === 3) {
    var p = ("slid on ")
} else if (e === 4) {
    var p = ("slipped on ")
}
var n = Math.floor(Math.random() * 6);
if (n === 0) {
    var q = ("a mountain ")
}
if (n === 1) {
    var q = ("a candlestick ")
} else if (n === 2) {
    var q = ("a raw potato ")
} else if (n === 3) {
    var q = ("a book ")
} else if (n === 4) {
    var q = ("a glass bottle ")
} else if (n === 5) {
    var q = ("water ")
} else if (n === 6) {
    var q = ("fish soup ")
}
var o = Math.floor(Math.random() * 3);
if (o === 0) {
    var v = ("in the dark")
}
if (o === 1) {
    var v = ("in the light")
}
if (o === 2) {
    var v = ("in the middle of nowhere")
}
if (o === 3) {
    var v = ("in a giant lemon")
}
document.write(a + p + q + v)
相关问题