JavaScript 刷題 —— 四

第一題:字符串轉換爲二進制

題目

a+b 的結果保存爲二進制字符串

代碼

function addBinary(a,b) {
  return (a+b).toString(2)
}

第二題:二維數組相加

題目

Visualization:

|1 2 3|     |2 2 1|     |1+2 2+2 3+1|     |3 4 4|
|3 2 1|  +  |3 2 3|  =  |3+3 2+2 1+3|  =  |6 4 4|
|1 1 1|     |1 1 3|     |1+1 1+1 1+3|     |2 2 4|

代碼

function matrixAddition(a, b){
  //TODO
  return a.map((item,index)=>item.map((itemI,i)=>itemI + b[index][i]))
}

第三題:判斷三邊能否構成三角形

題目

三個參數能否構成三角形

代碼

function isTriangle(a,b,c)
{
   return Math.max(a,b,c)*2 < a+b+c;
}

// 代碼區
// 思路一
function isTriangle(a,b,c)
{
   return a + b > c && a + c > b && c + b > a;
}

// 思路二
function isTriangle(a,b,c)
{
  [a, b, c] = [a, b, c].sort((x, y) => x-y);
  return a+b > c;
}

第四題:返回字符串中第一個有重複值的元素

題目

返回字符串中第一個有重複值的元素
firstDup(‘tweet’) => ‘t’
firstDup(‘like’) => undefined

代碼

function firstDup (s) {
  // 時間挺長的,還是換用for循環比較,匹配成功就退出
  return s.split('').filter((a,i,arr)=>i!==arr.lastIndexOf(a))[0]
}

第五題:循環加減乘除

題目

根據字母排序
提取數字進行計算
循環使用 加減乘除,對結果進行四捨五入
Examples :
“24z6 1x23 y369 89a 900b” = 89 + 900 - 123 * 369 / 246 = 1299
“24z6 1z23 y369 89z 900b” = 900 + 369 - 246 * 123 / 89 = 1414
“10a 90x 14b 78u 45a 7b 34y” = 10 + 45 - 14 * 7 / 78 + 90 - 34 = 60

代碼

// 原始思路
/**
這個代碼在處理隨機測試的時候,沒有通過,排序有亂
但是把隨機測試的例子拿到本地卻可以正常運行
*/
function doMath(s){
  //your code here
  const reg = (a,b)=>a.replace(/\d/g,'').charCodeAt(0) - b.replace(/\d/g,'').charCodeAt(0)
  return Math.round(s.split(' ').sort((a,b)=>reg(a,b))   // 排序
          .map(item=>+item.replace(/[A-z]/g,''))         // 提取數字
          .reduce((a,b,i,arr)=>{                         // 計算
            switch(i%4){
              case 1: return a+b;break;
              case 2: return a-b;break;
              case 3: return a*b;break;
              case 0: return a/b;
            }
          }))
 }
  
// 改成下面的寫法通過
function doMath(s){
  //字符串排序要注意,這裏的 sort() 不能保證原始排序
  const reg = (a,b)=>a.replace(/\d/g,'') == b.replace(/\d/g,'') 
            ? s.indexOf(a) - s.indexOf(b)
            : a.replace(/\d/g,'').charCodeAt(0) - b.replace(/\d/g,'').charCodeAt(0)
  return Math.round(s.split(' ').sort((a,b)=>reg(a,b))   // 排序
          .map(item=>+item.replace(/[A-z]/g,''))         // 提取數字
          .reduce((a,b,i,arr)=>{                         // 計算
            switch(i%4){
              case 1: return a+b;break;
              case 2: return a-b;break;
              case 3: return a*b;break;
              case 0: return a/b;
            }
          }))
  }

第六題:數組配對

題目

鞋子配對,0是左腳,1是右腳,後面是尺碼,判斷是否全部能配對
For:
shoes = [[0, 21],
[1, 23],
[1, 21],
[0, 23]]
the output should be true;

代碼

// 原始思路
function pairOfShoes(shoes) {
  //coding and coding..
  var i = 0;
  shoes.map(item=>item[1]+''+item[0]).forEach((a)=>{
    a.charAt(a.length - 1) == 0 ? i += +a : i -= +a - 1;
  })
  return shoes[1] ? !i : false 
}

// 第二種思路,給自己點個贊,答案區暫未發現更好的解法
const pairOfShoes = shoes => 
	shoes[1] ? !shoes.map(item=>item[0] ? +item[1] : -item[1]).reduce((a,b)=>a+b) : false 

第七題:所有單詞首字母大寫

題目

Examples:
toWeirdCase( “String” );//=> returns “StRiNg”
toWeirdCase( “Weird string case” );//=> returns “WeIrD StRiNg CaSe”

代碼

function toWeirdCase(string){
  //TODO
  let n = 0;
  return string.split('').map((a,i,arr)=>{
    a==' ' ? n = 0 : n++;
    return n%2 ? a.toUpperCase() : a.toLowerCase()
  }).join('')
}

第八題:比較字符串相同位置元素

題目

s1 = “6900690040”
s2 = “4690606946”
s3 = “9990494604”
比較s1和s2,0在下標3,6在下標4,4在下標8,也就是3個共有位置。
比較s1和s3,9在下標1,0在下標3,9在下標5,也就是3個共有位置。
比較s2和s3,9在下標2,0在下標3,也就是2個共有位置。
返回結果 8/30 = 0.26666*100
posAverage( “6900690040, 4690606946, 9990494604”) = 26.666

代碼

function posAverage(s) {
    // your code
    const arrS = s.split(', ').map(i=>i.split(''));
    let n = 0,m=0;
    
    for(let j=0;j<arrS.length;j++){    // 遍歷數組
      for(let k=j+1;k<arrS.length;k++){  // 遍歷後續的比較數組
        for(let i=0;i<arrS[0].length;i++){     // 遍歷比較的字符串
          if(arrS[j][i] == arrS[k][i]){
            n++
          }
          m++
        }
      }
    }
  return n/m*100    
}

第九題:只對數字數組的奇數進行排序

題目

只對數字數組中的奇數進行排序、
sortArray([5, 3, 2, 8, 1, 4]) == [1, 3, 2, 8, 5, 4]

代碼

function sortArray(array) {
  // Return a sorted array.
  const oddArr = array.filter(i=>i%2).sort((a,b)=>a-b)
  return array.length ? array.map(i=>i%2 ? oddArr.shift() : i) : array
}

第十題:獲取範圍內所有質數

題目

獲取 min 到 max 範圍內有多少個質數

代碼

// 代碼未提交,3456232 11218321內運行時,報超時
function countPrimes(min, max) {
  // return ??
  let n = -1;
  min = min < 2 ? 2 : min;
  for(let i=min;i<=max;i++){
    for(let j=2;j<=Math.sqrt(i);j++){
      if(i%j==0){
        n++;
        break
      }
    }
  }
  return max - min - n
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章