通过循环和数组减少If语句(Reduce If statement by looping and Arrays)

编程入门 行业动态 更新时间:2024-10-08 00:27:08
通过循环和数组减少If语句(Reduce If statement by looping and Arrays)

我试图缩短一些if语句

以前我有以下内容:

var regex = new RegExp(/[~`!#$%\^&*+=\-\[\]\\';,/{}|\\":<>\?()]/); var ltr = /[a-zA-Z ]+/; var mistakeA; var mistakeB; var mistakeC; var input = document.myform.myinputname.value; var input2 = document.myform.myinputname.value; if(ltr.test(input) && !(regex.test(input))){ myinputid.style.border ='1.5px solid red; mistakeA; } else if (regex.test(input) && !(ltr.test(input))){ myinputid.style.border ='1.5px solid red; mistakeB; } // same for input 2

所以基本上是检查输入是否正确。 到目前为止,所有工作都符合逻辑。 但是我有太多的If-Statement所以现在我试图将它们缩短如下:

var input [input1,input2,input3,input4]; for (i= 0; i < input.length; i++){ if (ltr.test(input[]) &&!(regex.test(input[]))){ myinputid.style.border='1.5px solid red'; } else { alert("noarray"); } }

那么我做错了什么? 或者我如何缩短我的If-Statements? 我有8个结构相同的陈述。 不调用数组(作为输入)。 它不起作用,因为.test(input [])必须在某种程度上是错误的。

I'm trying to shorten a couple of if-statement

Previously I had following:

var regex = new RegExp(/[~`!#$%\^&*+=\-\[\]\\';,/{}|\\":<>\?()]/); var ltr = /[a-zA-Z ]+/; var mistakeA; var mistakeB; var mistakeC; var input = document.myform.myinputname.value; var input2 = document.myform.myinputname.value; if(ltr.test(input) && !(regex.test(input))){ myinputid.style.border ='1.5px solid red; mistakeA; } else if (regex.test(input) && !(ltr.test(input))){ myinputid.style.border ='1.5px solid red; mistakeB; } // same for input 2

So basically it's a check if the input is correct. And everything worked so far logicalwise. But i had too many If-Statement so now i tried to shorten them as following:

var input [input1,input2,input3,input4]; for (i= 0; i < input.length; i++){ if (ltr.test(input[]) &&!(regex.test(input[]))){ myinputid.style.border='1.5px solid red'; } else { alert("noarray"); } }

So what am i doing wrong? Or how can i shorten my If-Statements? I have 8 Statements with the same structure. The array (which are the inputs ) aren't called. it's not working because the .test(input[]) must be somehow wrong.

最满意答案

var input = []; // that's how you declare an array input.push(document.myform.myinputname.value); // add a new item input.push(document.myform.myinputname.value); // add another // ... // Or: (notice =) //var input = [document.myform.myinputname.value, document.myform.myinputname.value, ...]; // just an advice (use var i instead of i) to not make i global for (var i = 0; i < input.length; i++){ if (ltr.test(input[i]) && !(regex.test(input[i]))){ // use subscripts to indicate which item of the array to test myinputid.style.border='1.5px solid red'; } else { alert("noarray"); } } var input = []; // that's how you declare an array input.push(document.myform.myinputname.value); // add a new item input.push(document.myform.myinputname.value); // add another // ... // Or: (notice =) //var input = [document.myform.myinputname.value, document.myform.myinputname.value, ...]; // just an advice (use var i instead of i) to not make i global for (var i = 0; i < input.length; i++){ if (ltr.test(input[i]) && !(regex.test(input[i]))){ // use subscripts to indicate which item of the array to test myinputid.style.border='1.5px solid red'; } else { alert("noarray"); } }

更多推荐

本文发布于:2023-08-07 12:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464050.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   语句   Reduce   Arrays   looping

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!