当前位置:网站首页>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
边栏推荐
- Stable and easy-to-use short connection generation platform, supporting API batch generation
- After the mailbox of the Pagoda Post Office is successfully set up, it can be sent but not received.
- 逍遥多开模拟器ADB驱动连接
- ES6数组的扩展方法map、filter、reduce、fill和数组遍历for…in for…of arr.forEach
- Batch replace file fonts, Simplified -> Traditional
- hackmyvm-bunny walkthrough
- PHP 给图片添加全图水印
- [mikehaertl/php-shellcommand] A library for invoking external command operations
- (6) 学生信息管理系统设计
- (2) 顺序结构、对象的布尔值、选择结构、循环结构、列表、字典、元组、集合
猜你喜欢
随机推荐
IP门禁:手把手教你用PHP实现一个IP防火墙
Using PHPMailer send mail
4. PHP array and array sorting
13.JS输出内容和语法
查询数据库中所有表的索引,并且解析成sql
Solve the problem of Zlibrary stuck/can't find the domain name/reached the limit, the latest address of Zlibrary
hackmyvm: controller walkthrough
After the mailbox of the Pagoda Post Office is successfully set up, it can be sent but not received.
PHP入门(自学笔记)
What are the killer super powerful frameworks or libraries or applications for PHP?
攻防世界—MISC 新手区1-12
(8) requests, os, sys, re, _thread
SQL分类、DQL(数据查询语言)、以及相应SQL查询语句演示
CTF入门笔记之ping
DVWA靶机安装教程
17. JS conditional statements and loops, and data type conversion
Shuriken: 1 vulnhub walkthrough
批量替换文件字体,简体->繁体
hackmyvm: kitty walkthrough
关于tp的apache 的.htaccess文件