正负转换

  1. function invert(array) {
  2. return array.map(item => {
  3. return -item
  4. });
  5. }
  6. var arr = [1, 23, 4, -5, -8]
  7. console.log(invert(arr))

翻转字符串

  1. function solution(str){
  2. return str.split('').reverse().join('')
  3. }
  4. console.log(solution("world"))

数组中某些值相加

指定某些条件的值进行相加

  1. function positiveSum(arr) {
  2. return arr.filter(x => {
  3. x > 0
  4. }).reduce((s, v) => {
  5. s += v
  6. }, 0)
  7. }
  8. var arr = [1, -4, 7, 12]
  9. console.log(positiveSum(arr))
  1. function positiveSum(arr) {
  2. return arr.reduce((a,b)=> a + (b > 0 ? b : 0),0);
  3. }

去除数组中最小与最大值并求和

  1. function sumArray(array) {
  2. return array ? array.sort((a, b) => {
  3. return a - b
  4. }).slice(1, -1).reduce((a, b) => {
  5. return a + b
  6. }, 0) : 0
  7. }
  8. var array = [6, 2, 1, 8, 10]
  9. console.log(sumArray(array))

整体求和在减去一个最大值与一个最小值

  1. function sumArray(array) {
  2. return Array.isArray(array) && array.length > 1
  3. ? array.reduce((s, n) => s + n, 0) - Math.min(...array) - Math.max(...array)
  4. : 0
  5. }

对传入的数字进行平方并相加求和

  1. function squareSum(numbers) {
  2. return numbers.map(item=>item*item).reduce((a,b)=>a+b,0)
  3. }
  4. var numbers = [1,2,3]
  5. console.log(squareSum(numbers))

对两个字符串 s1 与 s2 ,要求去除相同的部分,最终返回以 a-z 的顺序的字符串结果

  1. function longest(s1, s2) {
  2. // your code
  3. //组合 去重 排列
  4. let newStr = s1.concat(s2)
  5. let uniqStr = Array.from(new Set(newStr.split('')))
  6. let res = uniqStr.sort().join("")
  7. return res
  8. }
  9. let s1 = "xyaabbbccccdefww"
  10. let s2 = "xxxxyyyyabklmopq"
  11. console.log(longest(s1, s2))
  1. const longest = (s1, s2) => [...new Set(s1+s2)].sort().join('')
  1. function longest(s1, s2) {
  2. return Array.from(new Set(s1 + s2)).sort().join('');
  3. }

对给定数组中按照从小到大的顺序,(取出四位),求前两位进行相加的值

  1. function sumTwoSmallestNumbers(numbers) {
  2. let newNum = numbers.sort((a,b)=>{
  3. return a-b
  4. }).slice(0,4)
  5. return newNum[0] + newNum[1]
  6. }
  7. let numbers = [10, 343445353, 3453445, 3453545353453]
  8. console.log(sumTwoSmallestNumbers(numbers))
  1. function sumTwoSmallestNumbers(numbers) {
  2. var [ a, b ] = numbers.sort((a, b) => a - b)
  3. return a + b
  4. }

求给定数组中的平均值

  1. function find_average(array) {
  2. return array.reduce((a, b) => {
  3. return a + b
  4. }, 0) / array.length
  5. }
  6. let array = [1, 2, 3]
  7. console.log(find_average(array))

解析域名中的字符串

示例中的一些情况 Test.assertEquals(domainName(“http://google.com“), “google”); Test.assertEquals(domainName(“http://google.co.jp“), “google”);

Test.assertEquals(domainName(“www.xakep.ru”), “xakep”);

Test.assertEquals(domainName(“https://youtube.com“), “youtube”);

  1. function domainName(url) {
  2. let res
  3. if (url.includes("http")) {
  4. res = url.split("/")[2]
  5. } else {
  6. res = url
  7. }
  8. if (res.includes("www")) {
  9. return res.split(".")[1]
  10. } else {
  11. return res.split(".")[0]
  12. }
  13. }
  14. let url = "http://github.com/carbonfive/raygun"
  15. domainName(url)

replace 实现

  1. function domainName(url){
  2. url = url.replace("https://", '');
  3. url = url.replace("http://", '');
  4. url = url.replace("www.", '');
  5. return url.split('.')[0];
  6. };

正则表达式

  1. function domainName(url){
  2. return url.replace(/(https?:\/\/)?(www\.)?/, '').split('.')[0]
  3. }

知识储备

  • 去重
    • 字符串
    • 数组

https://blog.csdn.net/weihaifeng163/article/details/87854720