首页 > 解决方案 > 我应该如何编写一个测试来证明或反驳所做的陈述并将这些测试的结果存储在变量中?

问题描述

这只是一个练习题,我只是想知道它是如何完成的。我自己尝试过一件事,但对如何使它工作无济于事。

这是脚本:

<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}`;

不工作...

标签: javascript

解决方案


您应该直接分配比较的结果,而不是使用字符串。

也基于标签weightComparisonandpwdMatch应该被颠倒。

// 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>


推荐阅读