我如何携带功能结果备份功能以再次重申功能?
我有一个通过foreach循环传递的数组。然后将每个项目通过并检查特定订单或字符。如果是正确的,则该字符应从字符串中删除,则应该用新字符串将其重新循环。 (旧字符串减去检查字符)
我的问题是所有功能仍在本地持有旧字符串(在删除字符之前)。我如何将新字符串传递到函数中,而不重置当前保存的“ deletestringdata”值?
let dnaArray = ["ACATATAGACATACGT","AAAAAATACATAGTAGTCGGGTAG","ATACATCGGGTAGCGT"];
dnaStrand = "";
//SORT THROUGH EACH ITEM IN ARRAY
dnaArray.forEach((dnaStrand, index) => {
if (findDna(dnaStrand)) {
console.log("Case #" + index + " " + dnaStrand + ": YES");
}
else {
console.log("Case #" + index + " " + dnaStrand + ": NO");
};
});
function findDna(dnaStrand){
if (findHead(dnaStrand)){
if(findBody(dnaStrand)){
console.log("dna");
return true;
}
}
else {
return false;
}
};
function findHead(dnaStrand){
if (findGlobe(dnaStrand)){
if (findEyeSpots(dnaStrand)) {
return true;
}
}
else{
return false;
}
};
function findBody(dnaStrand){
if (findGlobe(dnaStrand) && findLegs(dnaStrand)) {
return true;
}
else {
return false;
}
};
function findGlobe(dnaStrand){
if(findMatch(dnaStrand, /(A+(TAC|CAT)A)/)){
return true;
}else{
console.log("No Globe");
}
};
function findEyeSpots(dnaStrand){
if(findMatch(dnaStrand, /T(CG*T)*AG/)){
return true;
}else{
console.log("No Eyes");
}
};
function findLegs(dnaStrand){
if(findMatch(dnaStrand, /CG*T/)){
return true;
}else{
console.log("No Legs");
}
};
function findMatch (dnaStrand, regex) {
dnaStrand = String(dnaStrand);
let isMatch = dnaStrand.match(regex);
isMatch = String(isMatch[0]);
//console.log(isMatch);
if (isMatch) {
deleteStringData(dnaStrand, isMatch);
return true;
}
else {
return false;
}
};
function deleteStringData (dnaStrand, string) {
dnaStrand = dnaStrand.replace(string, "");
};
I have an Array that is passed through a forEach loop. Each item is then passed through and is checked for a specific order or characters. If it is true the characters are meant to be removed from the string, then it is supposed to be looped back over with the new string. (Old string minus the checked characters)
My problem is the fact that all the functions are still locally holding the old String (Before the characters were removed). How can I pass the new string into the functions without resetting the value "deleteStringData" currently holds?
let dnaArray = ["ACATATAGACATACGT","AAAAAATACATAGTAGTCGGGTAG","ATACATCGGGTAGCGT"];
dnaStrand = "";
//SORT THROUGH EACH ITEM IN ARRAY
dnaArray.forEach((dnaStrand, index) => {
if (findDna(dnaStrand)) {
console.log("Case #" + index + " " + dnaStrand + ": YES");
}
else {
console.log("Case #" + index + " " + dnaStrand + ": NO");
};
});
function findDna(dnaStrand){
if (findHead(dnaStrand)){
if(findBody(dnaStrand)){
console.log("dna");
return true;
}
}
else {
return false;
}
};
function findHead(dnaStrand){
if (findGlobe(dnaStrand)){
if (findEyeSpots(dnaStrand)) {
return true;
}
}
else{
return false;
}
};
function findBody(dnaStrand){
if (findGlobe(dnaStrand) && findLegs(dnaStrand)) {
return true;
}
else {
return false;
}
};
function findGlobe(dnaStrand){
if(findMatch(dnaStrand, /(A+(TAC|CAT)A)/)){
return true;
}else{
console.log("No Globe");
}
};
function findEyeSpots(dnaStrand){
if(findMatch(dnaStrand, /T(CG*T)*AG/)){
return true;
}else{
console.log("No Eyes");
}
};
function findLegs(dnaStrand){
if(findMatch(dnaStrand, /CG*T/)){
return true;
}else{
console.log("No Legs");
}
};
function findMatch (dnaStrand, regex) {
dnaStrand = String(dnaStrand);
let isMatch = dnaStrand.match(regex);
isMatch = String(isMatch[0]);
//console.log(isMatch);
if (isMatch) {
deleteStringData(dnaStrand, isMatch);
return true;
}
else {
return false;
}
};
function deleteStringData (dnaStrand, string) {
dnaStrand = dnaStrand.replace(string, "");
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
抱歉,我有点忙,没有时间阅读您的所有代码。根据您的问题,我可以为您提供一些基本的指导。
确保您的功能采用参数。如果每次最终结果都不令人满意,或者您只想再次重复它,请执行if语句,如果您要重复它,请在内部进行
您的功能(endResult)
。如果您担心滞后,请考虑使最终结果成为全局变量,并将settimeout
设置为您的功能。Sorry, but I am sort of busy and don't have the time to read through all your code. Based on your question, I can give you some basic guidance.
make sure your function takes a parameter. If each time the end result is not satisfactory or you just want to repeat it again, do an if statement, and if you're going to repeat it, do
yourFunction(endresult)
inside. If you worry about lag, consider making your end result a global variable and setting asetTimeout
to your function.如果您在循环中声明测试功能,则可以在该范围中的单个实例上作用,而无需传递周围的任何参考。
您也可以使用测试。
这可能不是最有效的方法,但非常可读。
If you declare the test functions inside the loop you can act on single instance of the strand in that scope without needing to pass any references around.
You can also use Promises to chain the tests.
This may not be the most efficient method but it is very readable.