当前位置:网站首页>The roll call system and array elements find maximum and minimum values for sorting of objects
The roll call system and array elements find maximum and minimum values for sorting of objects
2022-08-02 03:59:00 【yorup】
I. Roll call system
1. Write the basic elements first, here is a brief write up
???
The initial effect with no styles added is as follows:
2. In order to look better, add some CSS styles below
.box{width: 200px;margin: auto;margin-top: 20px;}.box div{background: red;color: #fff;width: 100%;height: 80px;text-align: center;line-height: 80px;font-size: 18px;}.box button{width: 100%;height: 35px;text-align: center;margin-top: 10px;border: 0px;background: blue;color: #fff;}
The effect after adding is as follows
3. Next, click "Start" to realize the roll call system. The setinterval() timer is mainly used to realize the effect of automatic click, and the "start" and "pause" of the button are realized by marking true and false with flag.
var realnames = ["Zhang San", "Li Si", "Zhao Liu", "Wang Wu", "Lu Qi", "Wang Erma", "Dogleft"];var realname = document.getElementById("realname");var flag = false;var btn = document.getElementById("btn");function call(){flag = !flag;if(flag){window.myInter = setInterval(function(){var num = Math.floor(Math.random()*realnames.length);realname.innerHTML = realnames[num];},100);btn.innerText = "Pause";}else{clearInterval(window.myInter);//Clear the timerbtn.innerText = "Start";}}
The final effect is as follows
Second, the array elements are the elements of the object sorting to find the maximum and minimum age (two methods)
The arrays that need to find the maximum and minimum values are as follows
var data = [{realname:"Zhang San",age:19},{realname:"Wang 5 1",age:128},{realname:"Wang Wu 2",age:128},{realname:"Li Si",age:20},{realname:"Wang Wu",age:28},{realname:"Zhao Liu",age:19},];
Method 1: Use the sort() method
data.sort(function(a,b){return a.age - b.age;});console.log(data);var min = data[0];var max = data[data.length-1];
The results are as follows
At this point, you can see that there are multiple maximum and minimum values, and you need to output them all
for(var i=0;i
The effect is as follows
Method 2: Use a for loop to sort.Assuming that the age value of the first element is the largest (minimum), traverse each element in the array, and replace the initial value when there is an age value greater than the initial value.
var arrmax = [];var arrmin = [];var max = data[0];var min = data[0];for(var i=0; idata[i].age) {min = data[i];}}//Same as method 1 to get multiple maximum and minimum valuesfor(var i=0;i
Note: If you have any questions, please leave a comment
边栏推荐
- Warzone: 3 (Exogen) vulnhub walkthrough
- Multithreading (implementing multithreading, thread synchronization, producer and consumer)
- PHP实现搜索框的自动反查提示
- Eric靶机渗透测试通关全教程
- Masashi: 1 vulnhub walkthrough
- (3)Thinkphp6数据库
- TCP communications program
- Phpstudy安装Thinkphp6(问题+解决)
- 微信小程序开发视频加载:[渲染层网络层错误] Failed to load media
- (4) Function, Bug, Class and Object, Encapsulation, Inheritance, Polymorphism, Copy
猜你喜欢
Phonebook
IP access control: teach you how to implement an IP firewall with PHP
hackmyvm: again walkthrough
ES6数组的扩展方法map、filter、reduce、fill和数组遍历for…in for…of arr.forEach
PHP8.2的版本发布管理员和发布计划
DVWA drone installation tutorial
Praying: 1 vulnhub walkthrough
PHP有哪些杀手级超厉害框架或库或应用?
Thread Pool (Introduction and Use of Thread Pool)
Eric靶机渗透测试通关全教程
随机推荐
hackmyvm: kitty walkthrough
3. PHP data types, constants, strings and operators
PHP 发起支付宝支付时 订单信息乱码解决
3.PHP数据类型、常量、字符串和运算符
2.PHP变量、输出、EOF、条件语句
Turn trendsoft/capital amount of Chinese capital library
Introduction to PHP (self-study notes)
Several interesting ways to open PHP: from basic to perverted
SQL:DDL、DML、DQL、DCL相应介绍以及演示
php函数漏洞总结
[trendsoft/capital]金额转中文大写库
动力:2 vulnhub预排
1.初识PHP
4.表单与输入
16. JS events, string and operator
Orasi: 1 vulnhub walkthrough
[mikehaertl/php-shellcommand]一个用于调用外部命令操作的库
[league/flysystem] An elegant and highly supported file operation interface
PHP8.2中字符串变量解析的新用法
The focus of the Dom implementation input triggers