w3cschool js脚本初级实战所有题目 解题方法(自做)
作者:互联网
function reverseString(str) {
// for(var i in x)
return str.split('').reverse().join('');
}
function factorialize(num) {
if (num == 1 || num == 0) {
return 1;
}
return num * factorialize(num - 1);
}
function palindrome(str) {
// Good luck!
str = str.toLowerCase().replace(/[^a-z0-9]/gi, '');
return str.split('').reverse().join('') == str;
}
function findLongestWord(str) {
var wordList = str.split(' ');
for (var wordNum = 0, maxLength = 0; wordNum < wordList.length; wordNum++) {
if (wordList[wordNum].length > maxLength) {
maxLength = wordList[wordNum].length;
}
}
return maxLength;
}
function titleCase(str) {
var arr = str.split(' ');
for (var i = 0; i < arr.length; i++) {
arr[i] = arr[i].toLowerCase();
word = arr[i].split('');
word[0] = word[0].toUpperCase();
arr[i] = word.join('');
}
return arr.join(' ');
}
function largestOfFour(arr) {
// You can do this!
var maxArr = [];
for (var i = 0; i < arr.length; i++) {
var max = arr[i][0];
for (var j = 1; j < arr[i].length; j++) {
if (arr[i][j] > max) {
max = arr[i][j];
}
}
maxArr.push(max)
}
return maxArr;
}
function confirmEnding(str, target) {
// "Never give up and good luck will find you."
// -- Falcor
var endnum = target.length;
var arr = str.split('');
var strEndArr = [];
for (var i = arr.length - endnum; i < arr.length; i++) {
strEndArr.push(arr[i]);
}
return strEndArr.join('') == target;
}
function confirmEnding2(str, target) {
// "Never give up and good luck will find you."
// -- Falcor
return str.endsWith(target);
}
function repeat(str, num) {
// repeat after me
var newStr = '';
while (num > 0) {
newStr += str;
num -= 1;
}
return newStr;
}
function truncate(str, num) {
// Clear out that junk in your trunk
if (num >= str.length) {
return str;
}
var limitnum = num;
if (num > 3) {
limitnum = num - 3;
}
var arr = str.split('');
var newArr = [];
for (var i = 0; i < limitnum && i < arr.length; i++) {
newArr.push(arr[i]);
}
return newArr.join('') + '...';
}
function chunk(arr, size) {
// Break it up.
var newArrList = [];
for (var i = 0; i < arr.length;) {
var newArr = [];
for (var j = i; i < size + j && i < arr.length; i++) {
newArr.push(arr[i]);
}
newArrList.push(newArr);
}
return newArrList;
}
function slasher(arr, howMany) {
// it doesn't always pay to be first
var newArr = [];
if (howMany > arr.length) {
return newArr;
}
for (var i = howMany; i < arr.length; i++) {
newArr.push(arr[i]);
}
return newArr;
}
function findAlpha(arr) {
var Alpha = [];
var arrLength = arr.length;
for (var i = 0; i < arrLength; i++) {
var alphaLength = Alpha.length
var alpha = arr[i];
var flag = 0;
for (var j = 0; j < alphaLength; j++) {
if (alpha == Alpha[j]) {
flag = 1;
}
}
if (flag == 1) {
flag = 0;
continue;
} else {
Alpha.push(arr[i]);
}
}
return Alpha;
}
function compare(arr1, arr2) {
length1 = arr1.length;
length2 = arr2.length;
for (var i = 0; i < length1; i++) {
var alpha = arr1[i];
var flag = 0;
for (var j = 0; j < length2; j++) {
if (alpha == arr2[j]) {
flag = 1;
break;
}
}
if (flag === 0) {
return false;
}
}
return true;
}
function mutation(arr) {
arr1 = arr[0].toLowerCase().split('');
arr2 = arr[1].toLowerCase().split('');
arr3 = findAlpha(arr1);
arr4 = findAlpha(arr2);
return compare(arr4, arr3);
}
function bouncer(arr) {
// Don't show a false ID to this bouncer.
return arr.filter(item => item !== false && item !== null && item !== "" && item !== undefined && item !== 0 && !Number.isNaN(item));
}
// Number.isNaN(item)只判断对象是否正等于NaN,isNan(item)判断对象等于NaN或非数字
// isNaN('hello world'); // true
// Number.isNaN('hello world'); // false
function destroyer(arr) {
// Remove all the values
var length = arguments.length;
for (var i = 1; i < length; i++) {
arr = arr.filter(item => item != arguments[i]);
}
return arr;
}
function quickSort(arr) {
if (arr.length <= 1) {
return arr;
}
var low = [];
var high = [];
var pivot = arr.pop();
var length = arr.length;
for (var i = 0; i < length; i++) {
if (arr[i] < pivot) {
low.push(arr[i]);
} else {
high.push(arr[i]);
}
}
return quickSort(low).concat([pivot]).concat(quickSort(high));
}
function binarySearch(arr, target, low = 0, high = (arr.length)) {
var mid = Math.floor((high - low) / 2 + low);
if (target == arr[mid] || low == high) {
return (mid);
} else if (target < arr[mid]) {
return binarySearch(arr, target, low, mid);
} else {
return binarySearch(arr, target, mid + 1, high);
}
}
function where(arr, num) {
// Find my place in this sorted array.
return binarySearch(quickSort(arr), num);
}
function rot13(str) { // LBH QVQ VG!
var length = str.length;
var strArr = [];
for (var i = 0; i < length; i++) {
code = str.charCodeAt(i);
if (65 <= code && code <= 90) {
if (code + 13 > 90) {
code = code - 13;
} else {
code = code + 13;
}
}
strArr.push(String.fromCharCode(code));
}
return strArr.join('');
}
function sumAll(arr) {
var max = Math.max(arr[0], arr[1]);
var min = Math.min(arr[0], arr[1]);
var sum = 0;
for (var i = min; i < max + 1; i++) {
sum += i;
}
return sum;
}
function findOnly(arr1, arr2) {
var length1 = arr1.length;
var length2 = arr2.length;
var newArr = [];
for (var i = 0; i < length1; i++) {
var item = arr1[i];
var flag = 0;
for (var j = 0; j < length2; j++) {
if (item == arr2[j]) {
flag = 1;
break;
}
}
if (flag === 0) {
newArr.push(item);
} else {
flag = 0;
}
}
return newArr;
}
function diff(arr1, arr2) {
return findOnly(arr1, arr2).concat(findOnly(arr2, arr1));
}
function convertNumArr(num) {
var numArr = [];
while ((num / 10) > 1) {
numArr.push(num % 10);
num = Math.floor(num / 10);
}
numArr.push(num);
return numArr.reverse();
}
// 数字转罗马字
// numberBase = [1, 10, 100, 1000];
// numberFive = [5, 50, 500];
// I V X L C D M
// 1: I 10: X 100: C
// 2: II 20: XX 200: CC
// 3: III 30: XXX 300: CCC
// 4: IV 40: XL 400: CD
// 5: V 50: L 500: C
// 6: VI 60: LX 600: DC
// 7: VII 70: LXX 700: DCC
// 8: VIII 80: LXXX 800: DCCC
// 9: IX 90: XC 900: CM
function convertNumArr(num) {
var numArr = [];
var base = 1;
while ((num / 10) >= 1) {
numArr.push((num % 10) * base);
num = Math.floor(num / 10);
base *= 10;
}
numArr.push(num * base);
return numArr.reverse();
}
function findBaseAndNumber(num) {
var base = 1;
while ((num / 10) >= 1) {
num = Math.floor(num / 10);
base *= 10;
}
return [num, base];
}
function equalBase(base) {
var romanBase = ['I', 'X', 'C', 'M'];
var numberBase = [1, 10, 100, 1000];
var length = numberBase.length;
for (var i = 0; i < length; i++) {
if (base == numberBase[i]) {
return romanBase[i];
}
}
}
function equalFive(base) {
var romanFive = ['V', 'L', 'D'];
var numberFive = [5, 50, 500];
var length = numberFive.length;
var five = base * 5;
for (var i = 0; i < length; i++) {
if (five == numberFive[i]) {
return romanFive[i];
}
}
}
function equal(num) {
var arr = findBaseAndNumber(num);
num = arr[0];
var base = arr[1];
var romanBase = equalBase(base);
var romanFive = equalFive(base);
var romanNextBase = equalBase(base * 10);
switch (num) {
case 0:
return '';
case 1:
return romanBase;
case 2:
return romanBase.concat(romanBase);
case 3:
return romanBase.concat(romanBase).concat(romanBase);
case 4:
return romanBase.concat(romanFive);
case 5:
return romanFive;
case 6:
return romanFive.concat(romanBase);
case 7:
return romanFive.concat(romanBase).concat(romanBase);
case 8:
return romanFive.concat(romanBase).concat(romanBase).concat(romanBase);
case 9:
return romanBase.concat(romanNextBase);
default:
return '';
}
}
function convert(num) {
if (num > 3999) {
return 'Uncommon,Please input lower than 4000,thank you.'
}
var numArr = convertNumArr(num);
var roman = [];
var length = numArr.length;
for (var i = 0; i < length; i++) {
roman.push(equal(numArr[i]));
}
return roman.join('');
}
标签:function,arr,return,自做,js,length,num,w3cschool,var 来源: https://www.cnblogs.com/William_Huang/p/11833526.html