Home > front end >  Javascript tasks (100 yuan of WeChat reward), don't understand please add WeChat 13825600069
Javascript tasks (100 yuan of WeChat reward), don't understand please add WeChat 13825600069

Time:12-12

BXXX on behalf of td id, feels like EXCEL add and subtract formula, asked to write a function, the string representation of the td's Numbers add up.
There are five kinds of forms
string formula

 
Str1="=b199"
Str2="=b142 - b143"
Str3="=c18 + c78 + c84 + c125"
Str4="=b261 + b262 + b263 + b264 + b265 + b266 + b267 + b268 + b269 + b270 + b271 + b272 + b273 - b274"
Str5="=b2 +... + b17 "


 & lt; Table> 

The serial number & lt;/td> Value & lt;/td> The formula & lt;/td>


1 & lt;/td>


2 & lt;/td>

.



CodePudding user response:

Is not quite easy, can write out will point js, id 1 ~ 100, in order to loop for dom, remove the td value converted to the number of add and subtract

CodePudding user response:

A function need to deal with these 5 kinds of circumstances, at the same time the regular expression is not well. Don't come out,

Str1="=b199"
Str2="=b142 - b143"
Str3="=c18 + c78 + c84 + c125"
Str4="=b261 + b262 + b263 + b264 + b265 + b266 + b267 + b268 + b269 + b270 + b271 + b272 + b273 - b274"
Str5="=b2 +... + b17"

CodePudding user response:

Split () the near the b out what thing

CodePudding user response:

 
//now supports addition, subtraction, multiplication, division
Var str6='=b1 + c2 b2/c1 *'
Alert (strSplit (str6))
//the console. The log ()
The function strSplit (STR) {
If (STR [0]! {
='=')Return the STR
}
Var name_id='
Var sum='
Var types=[' + ', '-' and '*', '/']//modify the algorithm of support here
For (var I=1; i If (types. IndexOf (STR) [I]!=1) {
The sum +=getNum (name_id) + STR [I]
Name_id='
} else {
Name_id +=STR [I]
}
If (I==STR. Length - 1) {
The sum +=getNum (name_id)
}
}
Return the eval (sum)
}

The function getNum (id) {
Return the document. The getElementById (id). The innerText
}

CodePudding user response:

1: the first letter and number=replace null
2: the eval () is the result directly

CodePudding user response:

Simple processing:
 



<meta charset="utf-8"/& gt;
Formula test

<body>









<script>
Var f='=b261 + b262 + b263 + b264 + b265 + b266 + b267 + b268 + b269 + b270 + b271 + b272 + b273 b274 + 3600'.
The function CAL (formula) {
//if input is equal to the number of first remove
If (formula. The substring (0, 1)==='=') {
Formula=formula. The substring (1);
}
Each calculation unit//
Var cells=formula. The split (//* + -//g);
//split operator
Var operators=formula. The match (//* + -//g);
//the first unit
Formula=val (cells [0]);
//other units splicing operator
For (var I=1; i Formula +=operators [I - 1) + val (cells [I]);
}
//output string after
The console. The log (formula);
//output calculation results
The console. The log (eval (formula));
}
The function val (cell) {
//count as elements of the begin with a letter of id, if there are other can modify the judgment logic
//function, for example, will get braces...
If (/[A Za - z]/. The test (cell)) {
Var=dom document. GetElementById (cell);
The return of the dom. The innerText;
} else {
Return the cell;
}
}
CAL (f);
</script>

CodePudding user response:

Pretty simple, don't delete me, not water, is simple, fee brain cells

CodePudding user response:

 
//with regular
Var str6='=b1 + c2 b2/c1 *'
The console. The log (onSum (str6))
The function onSum (STR) {
If (STR [0]! {
='=')Return the STR
} else {
Var r=//* + -//g
STR=STR. The substring (1)
Var types=STR. Match (r)
Types? Types. Push (') : types=[']
Var data=https://bbs.csdn.net/topics/str.split (r). The map (id=> document. The getElementById (id). The innerText). The map ((v, I)=> v + types [I])
Return the eval (data. Join ("))
}
}

CodePudding user response:

 










  • Related