我想做一个值检查器,它将在提交值后发送我定义的响应。我的要求:
‘如果x= 1,x= 2,x=3,那么我会得到“未通过”的响应
如果x= 4,x=5,x= 6,x= 7,x=8,x=9,x=10响应“传递!”
如果x= 4-,x=5+,x=yes响应“不是一个数字”
如果x= 0,x= 11,x= -2响应“错误数据”
如果x为空(没有输入数据)响应“没有数据!”
当我不得不定义4-,5+,是的,因为我的函数在读取那些没有+和-符号的数字,并且给出了“传递”的答案,就像4和5没有+和-,请检查它,也许你有一些想法来修正它。
完整代码:https://codesandbox.io/s/cocky-black-7mezc?file=/ifelse.html:0-2245我的代码:
<h2>Ievadi datus</h2>
<span contenteditable="true"><p id="ievad"></p></span>
<button id="poga1">Check!</button>
<h2>Rezultāts</h2>
<br />
<span contenteditable="true"><p id="izv">Vispirms ievadi datus!</p></span>和Javascript:
var rezultats = '';
document.getElementById('poga1').onclick = function () {
let izv = document.getElementById('izv');
let vertejums = document.getElementById('ievad');
let rezultats = parseFloat(vertejums.textContent);
return nep(rezultats);
};
function nep(rezultats) {
if (rezultats <= 0 || rezultats > 10.0) {
izvads = 'Wrong data';
} else if (rezultats < 4.0) {
izvads = 'Not passed!';
} else if (rezultats <= 10.0) {
izvads = 'Passed!';
} else if ((rezultats == '4-', '5+', 'yes')) {
izvads = "It's not a number!";
} else {
izvads = 'There are no data!';
}
izv = document.getElementById('izv');
izv.innerHTML = izvads;
}会感谢你的帮助!
发布于 2021-12-16 00:52:00
/*
`If x = 1, x= 2, x=3, then i get response "Not passed"
If x = 4, x=5, x= 6, x= 7, x=8, x=9, x=10 response "passed!"
If x = 4-, x=5+, x=yes response "not a number"
If x= 0, x= 11, x= -2 response "wrong data"
If x is empty (No data entered) response "there are no data!" `
*/
document.getElementById("input").addEventListener("change", function() {
// default to no data
let message = "there are no data!";
const output = document.getElementById("output");
// get the value, this will be text - trim all leading and trailing spaces
const value = this.value.trim();
if (value !== "") {
// try to convert it to an integer
const numeric = parseInt(value);
// check if it's a number and if it matches what was entered
if (isNaN(numeric) || numeric != value) {
message = "not a number";
} else
if (numeric >= 1 && numeric <= 3) {
message = "not passed";
} else if (numeric >= 4 && numeric <= 10) {
message = "passed!";
} else {
message = "wrong data";
}
}
output.textContent = message;
});<output id="output"></output><br>
<label for="input">Input</label>
<input type="text" id="input">
https://stackoverflow.com/questions/70371679
复制相似问题