//栗子:提取字符串中的分数,汇总后算出平均分,并与每个分数比较,输出
var s="张三 56分,李四74分,王五92分,赵六84分";
var a=s.match(/\d+/g),sum=0;//提取数字
for (var i = 0; i < a.length; i++) {
sum+=parseFloat(a[i]);
}
var avg=sum/a.length;
function f() {
var n=parseFloat(arguments[1]);
return n+"分"+"("+((n>avg)?("超出平均分"+(n-avg)):("低于平均分"+(avg-n)))+"分)";
}
var s1=s.replace(/(\d+)分/g,f);
console.log(s1);
当前位置:网站首页>【JS】提取字符串中的分数,汇总后算出平均分,并与每个分数比较,输出
【JS】提取字符串中的分数,汇总后算出平均分,并与每个分数比较,输出
2022-07-05 10:17:00 【木子欢儿】
边栏推荐
- StaticLayout的使用详解
- SAP ui5 objectpagelayout control usage sharing
- Livedata interview question bank and answers -- 7 consecutive questions in livedata interview~
- 如何写出高质量的代码?
- 5g NR system architecture
- @SerializedName注解使用
- PHP solves the problems of cache avalanche, cache penetration and cache breakdown of redis
- 《通信软件开发与应用》课程结业报告
- Comparative learning in the period of "arms race"
- What is the most suitable book for programmers to engage in open source?
猜你喜欢
非技術部門,如何參與 DevOps?
pytorch输出tensor张量时有省略号的解决方案(将tensor完整输出)
What is the origin of the domain knowledge network that drives the new idea of manufacturing industry upgrading?
驱动制造业产业升级新思路的领域知识网络,什么来头?
@Serializedname annotation use
[论文阅读] CKAN: Collaborative Knowledge-aware Atentive Network for Recommender Systems
How do programmers live as they like?
5g NR system architecture
How does redis implement multiple zones?
到底谁才是“良心”国产品牌?
随机推荐
Learning note 4 -- Key Technologies of high-precision map (Part 2)
How can non-technical departments participate in Devops?
CSDN always jumps to other positions when editing articles_ CSDN sends articles without moving the mouse
MySQL digital type learning notes
How to write high-quality code?
Glide Mastery
Constrained layout flow
@SerializedName注解使用
Solution of ellipsis when pytorch outputs tensor (output tensor completely)
微信小程序中,从一个页面跳转到另一个页面后,在返回后发现页面同步滚动了
微信小程序触底加载与下拉刷新的实现
如何判断线程池已经执行完所有任务了?
The horizontally scrolling recycleview displays five and a half on one screen, lower than the average distribution of five
Have you learned to make money in Dingding, enterprise micro and Feishu?
Usage differences between isempty and isblank
Who is the "conscience" domestic brand?
Workmanager Learning one
How does redis implement multiple zones?
Learning Note 6 - satellite positioning technology (Part 1)
《通信软件开发与应用》课程结业报告