Javascript 我应该如何编写一个测试来证明或反驳所做的陈述,并将这些测试的结果存储在变量中?
这只是一个练习问题,我只是想知道它是如何完成的。我自己试过一件事,但在如何使它起作用方面没有效果 以下是脚本:Javascript 我应该如何编写一个测试来证明或反驳所做的陈述,并将这些测试的结果存储在变量中?,javascript,Javascript,这只是一个练习问题,我只是想知道它是如何完成的。我自己试过一件事,但在如何使它起作用方面没有效果 以下是脚本: <script> // Statement 1: The elephant weights less than the mouse let eleWeight = 1000; let mouseWeight = 2; // Statement 2: The Ostrich is taller than the duck let ostrichHeight = 2; let
<script>
// Statement 1: The elephant weights less than the mouse
let eleWeight = 1000;
let mouseWeight = 2;
// Statement 2: The Ostrich is taller than the duck
let ostrichHeight = 2;
let duckHeight = 0.3;
// Statement 3: The two passwords match
let pwd1 = 'stromboli';
let pwd2 = 'stROmBoLi'
let weightComparison;
let heightComparison;
let pwdMatch;
const section = document.querySelector('section');
let para1 = document.createElement('p');
let para2 = document.createElement('p');
let para3 = document.createElement('p');
let weightTest = weightComparison ? 'True — elephants weight less than mice!?' : 'False — of course an elephant is heavier than a mouse!';
let heightTest = heightComparison ? 'True — an ostrich is indeed taller than a duck!' : 'False — apparently a duck is taller than an ostrich!?';
let pwdTest = pwdMatch ? 'True — the passwords match.' : 'False — the passwords do not match; please check them';
para1.textContent = weightTest;
section.appendChild(para1);
para2.textContent = heightTest;
section.appendChild(para2);
para3.textContent = pwdTest;
section.appendChild(para3);
不起作用…您应该直接分配比较结果,而不是使用字符串 同样,根据标签,
权重比较
和pwdMatch
应该颠倒
//语句1:大象比老鼠重
让eleWeight=1000;
让mouseWeight=2;
//声明2:鸵鸟比鸭子高
鸵鸟高度=2;
让高度=0.3;
//语句3:两个密码匹配
设pwd1='stromboli';
设pwd2='stROmBoLi'
让weightComparison=eleWeight鸭子高度;
设pwdMatch=pwd1==pwd2;
const section=document.querySelector('section');
设para1=document.createElement('p');
设para2=document.createElement('p');
设para3=document.createElement('p');
让重量测试=重量比较?”真的-大象比老鼠重!?“:”错——大象当然比老鼠重;
让高度测试=高度比较?”没错——鸵鸟确实比鸭子高!:'错-显然鸭子比鸵鸟高;
让pwdTest=pwdMatch?”True-密码匹配。“:”False-密码不匹配;请核对一下",;
para1.textContent=重量测试;
第2节儿童(第1款);
para2.textContent=高度测试;
第2节(第2款);
para3.textContent=pwdTest;
第3节(第3款)代码>
你说“不工作”是什么意思?发生了什么以及应该发生什么?为什么要使用模板文本,将结果转换为字符串数据类型?只需执行weightComparison=eleWeight>mouseWeight
,它将指定一个布尔值(true或false)
let weightComparison = `${eleWeight > mouseWeight}`;
let heightComparison = `${ostrichHeight > duckHeight}`;
let pwdMatch = `${pwd1 !== pwd2}`;