1 回答
TA贡献1789条经验 获得超10个赞
这eligible = isEligible(str);
是一个逻辑错误。eligible
永远是undefined
因为isEligible()
没有返回值。(我不得不这么说,尽管你的评论表明你知道这一点)。
要解决此问题,请返回结果doesAddressMatchPhase()
并将返回结果存储.some()
在变量中。然后在.some()
调用之后立即测试该变量是否为真,如果是,则中断循环并立即返回该变量。否则,等到函数结束时返回默认 false。
这是工作解决方案:
var phaseOne = [
{
streetNumberLow: "1",
streetNumberHigh: "436",
streetName: "barnhart",
streetCode: "rd",
city: "waynesburo",
state: "va",
zipCode: "22980",
}
];
var phaseOneExt = [
{
streetNumberLow: "580",
streetNumberHigh: "1002",
streetName: "battlefield",
streetCode: "rd",
city: "fort defiance",
state: "va",
zipCode: "24437",
}
];
var phaseTwo = [
{
streetNumberLow: "1",
streetNumberHigh: "727",
streetName: "bailey",
streetCode: "rd",
city: "fort defiance",
state: "va",
zipCode: "24437",
}
];
var phaseTwoCares = [
{
streetNumberLow: "728",
streetNumberHigh: "996",
streetName: "bailey",
streetCode: "rd",
city: "fort defiance",
state: "va",
zipCode: "24437",
}
];
function parseAddressString(str) {
var matches = str.match(
/(?<streetNumber>\d+)\s+(?<streetName>[\w\s-]+)\s+(?<streetCode>\w+),?\s+(?<city>[\w\s-]+),?\s+(?<state>\w+),?\s+(?<zipCode>\d+)/
);
if (matches) {
return matches.groups;
}
return false;
}
function doesStringMatch(a, b) {
return a.toLowerCase().includes(b.toLowerCase());
}
function doesAddressMatchPhase(address, phase) {
return (
address &&
+address.streetNumber >= +phase.streetNumberLow &&
+address.streetNumber <= +phase.streetNumberHigh &&
doesStringMatch(address.streetName, phase.streetName) &&
doesStringMatch(address.streetCode, phase.streetCode) &&
doesStringMatch(address.city, phase.city) &&
doesStringMatch(address.state, phase.state) &&
doesStringMatch(address.zipCode, phase.zipCode)
);
}
function isEligible(str) {
var address = parseAddressString(str);
var phases = [phaseOne, phaseOneExt, phaseTwo, phaseTwoCares];
var x;
let found = false;
for (x = 0; x < phases.length; x++) {
found = phases[x].some(function (phase) {
return doesAddressMatchPhase(address, phase);
});
if(found) {
break;
}
}
return found;
}
function addressValidation() {
var str = document.getElementById("address").value,
eligible = isEligible(str);
document.getElementById("demo").innerHTML = eligible ? "true" : "false";
}
@import url('https://fonts.googleapis.com/css2?family=Bebas+Neue&family=Poppins:wght@400;700&display=swap');
/* Global */
* {
margin: 0;
padding: 0;
font-family: 'Bebas Neue', cursive;
}
#address-checker {
height:25vh;
width: 60%;
margin: auto;
margin-top: -3vh;
padding: 3vw;
background-color: #fff;
position: relative;
z-index: 1;
box-shadow: 0 .5rem 1rem rgba(0, 0, 0, 0.5);
}
.checker-container {
height: 25vh;
}
.address-header {
padding: 3vh 0 2vh 0;
text-align: center;
color: #2277AE;
letter-spacing: 1px;
font-size: clamp( 1.5rem, 3.5vw, 3.5rem);
}
.address-checker-input {
padding: 1vh 0;
}
.address-box {
margin: 1.5vh 0 0 10vw ;
width: 60%;
padding: .75rem 0 .75rem .5rem;
border-radius: 10px;
border: #2277AE 2px solid;
color: #2277AE;
}
.address-box:hover {
border-color: #FEA00B;
transition: .2s;
}
.go-btn {
background-color: #2277AE;
padding: .75rem;
border-radius: 10px;
border: #2277AE 2px solid;
color: #fff;
}
.go-btn:hover {
border-color: #FEA00B;
background-color: #FEA00B;
transition: .2s;
}
<html>
<head>
<link rel="stylesheet" href="style.css">
</head>
<body>
<section id="address-checker">
<div class="checker-container">
<h1 class="address-header">
Is fiber to the home available for you?
</h1>
<div class="address-checker-input">
<input
id="address"
type="text"
name="searchaddress"
placeholder="Street Address, City, State, Zip Code"
class="address-box"
value="200 barnhart rd waynesburo, va 22980"
/>
<button class="go-btn" onclick="addressValidation()">Go</button>
</div>
<p id="demo"></p>
</div>
</section>
</body>
<script src="main.js"></script>
</html>
添加回答
举报