两道编程题:
- 将一个圆向右移动100px,并放大2为两倍(没做出来,宽高设置错了)
<style>
body {
margin: 0;
}
#circle {
width: 40px;
height: 40px;
border-radius: 50%;
background-color: aquamarine;
margin: 20px 0;
}
.circle {
background-color:blueviolet;
}
</style>
<body>
<div id="circle" class="circle">
</div>
<script>
const div = document.getElementById("circle");
let i = 0;
const interval = setInterval(function(){
if(i >= 10){
clearInterval(interval);
return;
}
const offsetTop = div.offsetTop;
div.style.width = div.offsetWidth + 4 + "px";
div.style.height = div.offsetHeight + 4 + "px";
div.style.marginLeft = 10 * (i + 1) + "px";
div.style.marginTop = offsetTop - 2 + "px";
div.style.marginBottom = offsetTop - 2 + "px";
i++;
}, 1000);
</script>
</body>
- 千分位分割,正则表达式和循环两种方式
function cut(num) {
return num && num.toString().replace(/\d+/, function(s){
return s.replace(/(\d)(?=(\d{3})+$)/g, '$1,')
})
}
function cut2(num) {
num = num.toString();
let res = "";
let temp = 0;
for (let i = num.length - 1; i >= 0; i--) {
if (temp == 3) {
res = num[i] + ',' + res;
temp = 1;
} else {
res = num[i] + res;
temp++;
}
}
return res;
}