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 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);
我试过了:
let weightComparison = `${eleWeight > mouseWeight}`;
let heightComparison = `${ostrichHeight > duckHeight}`;
let pwdMatch = `${pwd1 !== pwd2}`;
不工作...
解决方案
您应该直接分配比较的结果,而不是使用字符串。
也基于标签weightComparison
andpwdMatch
应该被颠倒。
// 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 = eleWeight < mouseWeight;
let heightComparison = ostrichHeight > duckHeight;
let pwdMatch = pwd1 === pwd2;
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);
<section></section>
推荐阅读
- spring-boot - 文档解析器的最佳实践
- reactjs - 迭代通用枚举类型 Typescript
- excel - 未找到名称的元素
- javascript - 在javascript中使用字节(UCS2)类型修改jpg图像Exif
- html - 如何将所有设备的特定位置的元素设置为响应式
- css - 绝对位置在 flex 的子级中不起作用
- python-3.x - Python列表在复制时相互同步
- reactjs - 'TypeError: 为购物车创建函数时无法读取未定义的属性'长度'。这是代码片段。可能是什么问题?
- javascript - 如何用wordpress中的特定类替换p标签中的内容?
- reactjs - 在使用 MovieDB 进行 fetch 时,我的 CPU 100% React