加法伺候
//超过20位数值相加----------------------------------------
function bigNumAdd(a, b) {if (!(typeof a === "string" && typeof b === "string")) return console.log("传入参数必须为字符串类型");if (a.includes("+") || a.includes("-") || b.includes("+") || b.includes("-")) return console.log("传参不要夹带正负符号+、-");var aArr = a.toString().split("");var bArr = b.toString().split("");var temporary = []; //临时值//比较大小长度,aArr的值要比bArr的值要大if (bArr.length > aArr.length) {//实现两值的调换temporary = aArr;aArr = bArr;bArr = temporary;} else if (bArr.length === aArr.length) {//比较数组里面的值var flag = true; //默认aArr比bArr的值大var length = bArr.length;for (var i = 0; i < length; i++) {var numObj = parseInt(aArr[i]);var addObj = parseInt(bArr[i]);if (numObj > addObj) {break;} else if (numObj < addObj) {flag = false;break;}}if (!flag) {//实现两值的调换temporary = aArr;aArr = bArr;bArr = temporary;}}for (var i = aArr.length - 1; i >= 0; i--) { //从后面算起var numObj = parseInt(aArr[i]);var addObj = parseInt(bArr[i - (aArr.length - bArr.length)]);if (!isNaN(addObj)) {if (numObj + addObj >= 10) {aArr[i] = numObj + addObj - 10;if (i - 1 < 0) {//如果是90+10的话,超出了数组的范围aArr.splice(0, 0, 1);} else {for (var j = i - 1; j >= 0; j--) { //从后面算起var obj = parseInt(aArr[j]);if (obj + 1 === 10) {aArr[j] = 0;if (j === 0) {//循环到最后一个,将aArr数组加上最前面1// splice(position, aOfItemsToRemove, item)// 拼接函数(索引位置, 要删除元素的数量, 元素)aArr.splice(0, 0, 1);i++; //数组的长度已经改变,将循环再次加多一次break;}} else {aArr[j] = obj + 1;break;}}}} else {aArr[i] = numObj + addObj;}}}return aArr.join("");
}console.log(bigNumAdd("999999999999999999999999", "999999999999999999999998"));//1999999999999999999999997
console.log(bigNumAdd("999999999999999999999999", "99999999999999999999998"));//1099999999999999999999997
console.log(bigNumAdd("1", "999999999999999999999999"));//1000000000000000000000000
console.log(bigNumAdd("1000000000000000000000000", "1"));//1000000000000000000000001
减法
//超过20位数值相减----------------------------------------
function bigNumSub(a, b) {if (a === b) return "0";if (!(typeof a === "string" && typeof b === "string")) return console.log("传入参数必须为字符串类型");if (a.includes("+") || a.includes("-") || b.includes("+") || b.includes("-")) return console.log("传参不要夹带正负符号+、-");function lt(a, b) {if (a.length < b.length) {return true;} else if (a.length === b.length) {return a < b;} else {return false;}}let isMinus = false;if (lt(a, b)) {[a, b] = [b, a];isMinus = true;}let len = Math.max(a.length, b.length);a = a.padStart(len, 0);b = b.padStart(len, 0);let flag = 0,result = "",temp;for (let i = len - 1; i >= 0; i--) {temp = parseInt(a[i]) - flag - parseInt(b[i]);if (temp < 0) {result = 10 + temp + result;flag = 1;} else {result = temp + result;flag = 0;}}result = (isMinus ? "-" : "") + result.replace(/^0+/, "");return result;
}console.log(bigNumSub("999999999999999999999999", "999999999999999999999998"));//1
console.log(bigNumSub("999999999999999999999999", "99999999999999999999998"));//900000000000000000000001
console.log(bigNumSub("1", "999999999999999999999999"));//-999999999999999999999998
console.log(bigNumSub("1000000000000000000000001", "1000000000000000000000000"));//1
#include
题目:表示数值的字符串 请实现一个函数用来判断字符串是否表示数值(包括整数和小数)。例如,字符串"+100"、"5e2"、"-123"、"3.1416"、"0123"及"-1E-16"都表示数值,但"12e"、"1a3.14"、"1.2.3"、"+-5"及"12e+5.4"都不是。 解题: 数值错误的形式有多种多样,但是正确的...
业务场景: 从中文字句中匹配出指定的中文子字符串 .这样的情况我在工作中遇到非常多, 特梳理总结如下. 难点: 处理GBK和utf8之类的字符编码, 同时正则匹配Pattern中包含汉字,要汉字正常发挥作用,必须非常谨慎.推荐最好统一为utf8编码,如果不是这种最优情况,也有酌情处理. 往往一个具有普适性的正则表达式会简化程...
简单record 一下
#include
//获取某一个cookie的值 const getCookie = key => {var k = key, dc = document.cookie;if (dc.length > 0) {var s = dc.indexOf(k + "=");if (s != -1) {s = s + k.length + 1;var e = d...
var SGheadMapPoints = {/*obj={ maxLng: minLng: maxLat: minLat: maxCount:最大人数 minCount:最小人数 total:点位数量 }*/get: function (obj) {var arr = [];obj.maxCount || (obj.maxCount...
创建index.html代码如下
//自动搜索指定的请柬 var alertTipText = "请柬找到了,就在这个网页里面,自己仔细看吧"; var delay = 1 * 1000;//1秒后循环下一页寻找 /*获取子DOM元素在父元素里面的索引位置(是第几个元素)*/ function getNodeListIndex(childNode) {return c...
获取天气情况(不支持跨域) /*json原生获取*/ function getJSON() {var XML;var url = "http://wthrcdn.etouch.cn/weather_mini?city=杭州";if (window.XMLHttpRequest) {XML = new XMLHttpRequest(...