坚持下去,编程是一门艺术,与君共勉!!!
function sumAll(arr) {
var result = 0;
var sn = Math.min(arr[0] , arr[1]);
var en = Math.max(arr[0] , arr[1]);
/************使用reduce只能在数组元素之间求和,对于数组中没有的数操作不了
* 如果享用这种方法解决问题需要把两个数字之间包括这两个数字全部存进一个新数组里对新数组求和
* var newArr = [];
for (var i = 0; i <= en - sn; i++) {
newArr[i] = i + sn;
}
result = newArr.reduce(function(prev ,cur){
return prev + cur;
},0);
*/
for (var i = sn; i <= en; i++) { //黑猫白猫,能抓老鼠就是好猫
result += i;
}
return result;
} sumAll([1, 5]);
1-区间求值
function diff(arr1, arr2) {
var newArr = []; pick(arr1,arr2); //两个数组都要遍历,算法写起来容易,但是时间复杂度很大
pick(arr2,arr1);
function pick(a1,a2){ //编写函数将两个数组中不同的元素放入新数组
for (var i in a1) {
if (a2.indexOf(a1[i]) < 0) {
newArr.push(a1[i]);
}
}
}
return newArr;
} diff([1, 2, 3, 5], [1, 2, 3, 4, 5]);
2-找出数组差异
function convert(num) {
var one = ["I","II","III","IV","V","VI","VII","VIII","IX"];
var ten = ["X","XX","XXX","XL","L","LX","LXX","LXXX","XC"];
var hundred = ["C","CC","CCC","CD","D","DC","DCC","DCCC","CM"];
var thousand = "M";
var result = "";
var th = Math.floor(num / 1000);
var h = Math.floor((num % 1000) / 100);
var t = Math.floor((num % 100) / 10);
var o = Math.floor(num % 10);
for (var i = 0; i < th; i++) {
result += thousand;
}
if(h){
result += hundred[h - 1];
}
if(t){
result += ten[t - 1];
}
if(o){
result += one[o - 1];
} return result;
} convert(36);
3-数字转罗马数字
function where(collection, source) { /*******网友给出的更优解*********
var arr = [] ;
var status = null;
for (var i = 0; i < collection.length; i++) {
for(var imp in source){
if (source[imp] !== collection[i][imp]) {
status = 0;
break;
}
status = 1;
}
if(status == 1){
arr.push(collection[i]);
}
}
**********/ // console.log(Object.getOwnPropertyNames(source).length);
//获取对象属性个数
var arr = [], index = 0;
for (; index < collection.length; index++) {
for (var key in collection[index]) {
var count = 0;
for (var key2 in source) {
if (collection[index].hasOwnProperty(key2)) {
if (source[key2] == collection[index][key2]) {
count++;
}
if (count == Object.getOwnPropertyNames(source).length && key == key2) {
arr.push(collection[index]);
}
}
}
}
}
return arr;
} where([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });
4-对象搜索
function myReplace(str, before, after) {
/*
*本方法只适用于字符串首字母大写的情况
*如果涉及到目标字符串中其他字符大写则需要对temp数组进行遍历逐一比对
*在if外层嵌套for循环并设置temp数组中的索引变量即可解决
*/
var arr = str.split(" "); //把字符串按单词转化成数组
var index = arr.indexOf(before); //找到目标单词在数组中的索引
var temp1 = arr[index].split(""); //数组中的目标单词按字母分割成新数组
if(/[A-Z]/.test(temp1[0])){ //新数组中首字母大写判定
after = after.charAt(0).toUpperCase() + after.slice(1); //把用来替换的字符串首字符改成大写
}
arr[index] = arr[index].replace(arr[index] , after); //字符串替换
str = arr.join(" "); //数组中的单词以空格为间隔拼接成字符串
return str;
} myReplace("Let us go to the store", "store", "mall");
5-字符串查询替换
function translate(str) {
var vowel = ["a", "e", "i", "o", "u"];
if (vowel.indexOf(str[0]) != -1) {
return str + "way";
}
while (vowel.indexOf(str[0]) == -1) {
str = str.substr(1) + str.substr(0, 1);
}
return str + "ay";
} translate("consonant");
6-字符串移动插入
function pair(str) {
var arr = new Array(); //注意:这里的数组定义方式是实例化定义
str = str.split("");
for (var i in str) {
arr[i] = new Array();
arr[i].push(str[i]);
if (str[i] == "G" || str[i] == "C") {
(arr[i] == "G") ? arr[i].push("C") : arr[i].push("G");
} else {
(arr[i] == "A") ? arr[i].push("T") : arr[i].push("A");
}
}
return arr;
} pair("GCG");
7-字符配对
8
function fearNotLetter(str) {
/*******下面这种网友给出的方法只符合缺失一个字符的情况
* 如果缺失多个则只能输出缺失的第一个
var sub;
for (var index = 0; index < str.length - 1; index++){
sub = str.charCodeAt(index + 1) - str.charCodeAt(index);
if (sub == 1) {
if (index == str.length) {
return undefined;
}
} else {
return String.fromCharCode(str.charCodeAt(index) + 1);
}
}
*/ //下面本人字节写的可以返回缺失一段多个字符的情况,但是不符合缺失多端字符的情况如abeg
//缺失多段字符的方法可以将内层for循环写成功能函数,在外层循环中每当发现sub!=1时去调用即可
//调用结束过后使用continue让外层循环继续执行直到整个str都遍历完为止
var sub = 0;
var index = [];
for (var i = 0; i < str.length - 1; i++) {
sub = str.charCodeAt(i + 1) - str.charCodeAt(i);
if (sub == 1) {
if(i == str.length){
return undefined;
}
} else {
for(var j = 0 ; j < sub - 1; j++){
index[j] = String.fromCharCode(str.charCodeAt(i)+1+j);
return index[j];
}
}
}
} fearNotLetter("abeg");
-字符串查询补充
9
function boo(bool) {
// What is the new fad diet for ghost developers? The Boolean.
//这种情况只能做检查提示,开发过程中除了检查提示还应该让用户重新输入直到正确为止
//可以使用try catch结构处理输入异常,或使用while函数让用户输入直到输入正确为止
if (bool === true || bool === false) {
return true;
} else {
return false;
}
} boo(null);
-输入检查
10
function unite(arr1, arr2, arr3) { var args = Array.from(arguments);
var arr = args.reduce(function(prev,cur){
return prev.concat(cur);
});
return arr.filter(function(item,index,arr){
return arr.indexOf(item) === index;
});
} unite([1, 3, 2], [5, 2, 1, 4], [2, 1]);
-数组去重
function convert(str) {
// :)
var arr = str.split("");
for (var i = 0; i < str.length; i++) {
switch (arr[i]) {
case '&':
arr[i] = "&";
break;
case '<':
arr[i] = "<";
break;
case '>':
arr[i] = ">";
break;
case '"':
arr[i] = """;
break;
case "'":
arr[i] = "'";
break;
}
}
return arr.join("");
} convert("Dolce & Gabbana");
11-html符号转实体
function spinalCase(str) { var array=str.split(" ");
if(array.length<=1){
array=str.split("_");
}
if(array.length<=1){
return str.replace(/([A-Z])/g,"-$1").toLowerCase();
}
str=array[0].toLowerCase();
for(var i=1;i<array.length;i++){
str=str.concat('-').concat(array[i].toLowerCase());
}
return str;
} spinalCase('This Is Spinal Tap');
12-字符串连接
function sumFibs(num) {
var fibo = [1, 1];
var nums = 2;
var temp = 0;
while(true){
temp = fibo[0] + fibo[1];
if(temp > num){
return nums;
}
if(temp % 2 !== 0){
nums += temp;
}
fibo[0] = fibo[1];
fibo[1] = temp;
}
} sumFibs(1000);
13-斐波那契
function sumPrimes(num) {
var sum = 0;
for(var i = 2; i <= num; i++){
var j = 2;
for( ; i%j !== 0; j++){}
if(i == j){
sum += i;
}
}
return sum;
} sumPrimes(10);
14-质数求和