| |

VerySource

 Forgot password?
 Register
Search
View: 885|Reply: 2

js string filtering problem

[Copy link]

2

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-3-20 16:30:01
| Show all posts |Read mode
My function received a string such as 789-456-1101 how to filter out "-"
Becomes 789,456,101
The book above is stored in a string
Then I got this book how to multiply each 2 and then add up
Such as 7 * 2 + 8 * 2 + 9 * 2 + 4 * 2 + 5 * 2 + 6 * 2 + 1 * 2 + 1 * 2 + 0 * 2 + 1 * 2

Thank you! By the way, is this a string or a character array?

And don't just talk about what to do, write a short code for reference!
Reply

Use magic Report

0

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-6-26 21:00:01
| Show all posts
Go to "-" you can do it yourself, you can use replace or split

var str[] = "787895610".split("|");

var sb = "";
for(var i=0;i<str.length;i++){
    sb = sb + str[i] + "* 2"
    if(i != str.length-1){
          sb = sb + "+";
    }
}
var result = eval(sb);
Reply

Use magic Report

0

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-7-12 11:15:01
| Show all posts
There are shorter

var str = "7875895101";
var result = eval(str.split("|").join.("*2+"));
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list