国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專(zhuān)欄INFORMATION COLUMN

16 道 JavaScript 基礎(chǔ)算法 - freeCodeCamp

yintaolaowanzi / 3004人閱讀

#1. Reverse a String

Reverse the provided string.

You may need to turn the string into an array before you can reverse it.

Your result must be a string.

function reverseString(str/*:string*/) {
  if (str.length <= 1) return str;
  let result = "";
  for (let i = str.length - 1; i >= 0; i--) {
    result += str.charAt(i);
  }
  return result;
}

reverseString("hello");
#2. Factorialize a Number

Return the factorial of the provided integer.

If the integer is represented with the letter n, a factorial is the product of all positive integers less than or equal to n.

Factorials are often represented with the shorthand notation n!.

For example: 5! = 1 * 2 * 3 * 4 * 5 = 120.

function factorialize(num/*:number*/) {
  if (num === 0) return 1;
  let result = num;
  while (--num > 0) {
    result *= num;
  }
  return result;
}

factorialize(5);
#3. Check for Palindromes

Return true if the given string is a palindrome. Otherwise, return false.

A palindrome is a word or sentence that"s spelled the same way both forward and backward, ignoring punctuation, case, and spacing.

Note

You"ll need to remove all non-alphanumeric characters (punctuation, spaces and symbols) and turn everything lower case in order to check for palindromes.
We"ll pass strings with varying formats, such as "racecar", "RaceCar", and "race CAR" among others.
We"ll also pass strings with special symbols, such as "2A3*3a2", "2A3 3a2", and "2_A3*3#A2".

function palindrome(str/*:string*/) {
  const lstr = str.toLowerCase();
  let low = 0, high = lstr.length - 1;
  const reg = /[^0-9a-z]/;
  while (low < high) {
    const lchar = lstr.charAt(low);
    if (reg.test(lchar)) {
      low++;
      continue;
    }
    const hchar = lstr.charAt(high);
    if (reg.test(hchar)) {
      high--;
      continue;
    }
    if (lchar !== hchar) return false;
    low++;
    high--;
  }
  return true;
}

palindrome("eye");
#4. Find the Longest Word in a String

Return the length of the longest word in the provided sentence.

Your response should be a number.

function findLongestWord(str/*:string*/) {
  let max = 0, len = 0;
  for (let i = 0; i < str.length; i++) {
    if (str.charAt(i) === " ") {
      if (len > max) max = len;
      len = 0;
    } else len++;
  }
  return Math.max(max, len);
}

findLongestWord("The quick brown fox jumped over the lazy dog");
#5. Title Case a Sentence

Return the provided string with the first letter of each word capitalized. Make sure the rest of the word is in lower case.

For the purpose of this exercise, you should also capitalize connecting words like "the" and "of".

function titleCase(str/*:string*/) {
  let result = "", len = 0;
  for (let i = 0; i < str.length; i++) {
    if (str.charAt(i) === " ") {
      if (len === 0) result += " ";
      else {
        result += str.charAt(i - len).toUpperCase()
          + str.substr(i - len + 1, len).toLowerCase();
        len = 0;
      }
    } else len++;
  }
  if (len === 0) return result;
  return result + str.charAt(len = str.length - len).toUpperCase()
    + str.substr(len + 1).toLowerCase();
}

titleCase("I"m a little tea pot");
#6. Return Largest Numbers in Arrays

Return an array consisting of the largest number from each provided sub-array. For simplicity, the provided array will contain exactly 4 sub-arrays.

Remember, you can iterate through an array with a simple for loop, and access each member with array syntax arr[i].

function largestOfFour(arrs/*:number[][]*/) {
  const results = [];
  for (const arr of arrs) {
    results.push(arr.reduce((max, current) => Math.max(max, current)));
  }
  return results;
}

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
#7. Confirm the Ending

Check if a string (first argument, str) ends with the given target string (second argument, target).

This challenge can be solved with the .endsWith() method, which was introduced in ES2015. But for the purpose of this challenge, we would like you to use one of the JavaScript substring methods instead.

function confirmEnding(str/*:string*/, tar/*:string*/) {
  const delta = str.length - tar.length;
  if (delta < 0) return false;
  for (let i = tar.length; i >= 0; i--) {
    if (str.charAt(i + delta) !== tar.charAt(i)) return false;
  }
  return true;
}

confirmEnding("Bastian", "n");
#8. Repeat a string

Repeat a given string (first argument) num times (second argument). Return an empty string if num is not a positive number.

function repeatStringNumTimes(str/*:string*/, num/*:number*/) {
  if (num <= 0) return "";
  if (num === 1) return str;
  if (num % 2) return str
    + repeatStringNumTimes(str, num - 1);
  str = repeatStringNumTimes(str, num >> 1);
  return str + str;
}

repeatStringNumTimes("abc", 3);
#9. Truncate a string

Truncate a string (first argument) if it is longer than the given maximum string length (second argument). Return the truncated string with a ... ending.

Note that inserting the three dots to the end will add to the string length.

However, if the given maximum string length num is less than or equal to 3, then the addition of the three dots does not add to the string length in determining the truncated string.

function truncateString(str/*:string*/, num/*:number*/) {
  if (str.length <= num) return str;
  if (num > 3) return str.substr(0, num - 3) + "...";
  return str.substr(0, num) + "...";
}

truncateString("A-tisket a-tasket A green and yellow basket", 11);
#10. Chunky Monkey

Write a function that splits an array (first argument) into groups the length of size (second argument) and returns them as a two-dimensional array.

function chunkArrayInGroups(arr/*:any[]*/, size/*:number*/) {
  const results = [];
  let start = 0;
  while (start < arr.length) {
    results.push(arr.slice(start, start = start + size));
  }
  return results;
}

chunkArrayInGroups(["a", "b", "c", "d"], 2);
#11. Slasher Flick

Return the remaining elements of an array after chopping off n elements from the head.

The head means the beginning of the array, or the zeroth index.

function slasher(arr/*:any[]*/, howMany/*:number*/) {
  return arr.slice(howMany);
}

slasher([1, 2, 3], 2);
#12. Mutations

Return true if the string in the first element of the array contains all of the letters of the string in the second element of the array.

For example, ["hello", "Hello"], should return true because all of the letters in the second string are present in the first, ignoring case.

The arguments ["hello", "hey"] should return false because the string "hello" does not contain a "y".

Lastly, ["Alien", "line"], should return true because all of the letters in "line" are present in "Alien".

function mutation(arr/*:any[]*/) {
  const str = arr[0].toLowerCase();
  const tar = arr[1].toLowerCase();
  for (let i = tar.length - 1; i >= 0; i--) {
    if (str.indexOf(tar.charAt(i)) < 0) return false;
  }
  return true;
}

mutation(["hello", "hey"]);
#13. Falsy Bouncer

Remove all falsy values from an array.

Falsy values in JavaScript are false, null, 0, "", undefined, and NaN.

function bouncer(arr/*:any[]*/) {
  return arr.filter((item) => item);
}

bouncer([7, "ate", "", false, 9]);
#14. Seek and Destroy

You will be provided with an initial array (the first argument in the destroyer function), followed by one or more arguments. Remove all elements from the initial array that are of the same value as these arguments.

function destroyer(arr/*:any[]*/, ...rest/*:any[]*/) {
  return arr.filter((item) => rest.indexOf(item) < 0);
}

destroyer([1, 2, 3, 1, 2, 3], 2, 3);
#15. Where do I belong

Return the lowest index at which a value (second argument) should be inserted into an array (first argument) once it has been sorted. The returned value should be a number.

For example, getIndexToIns([1,2,3,4], 1.5) should return 1 because it is greater than 1 (index 0), but less than 2 (index 1).

Likewise, getIndexToIns([20,3,5], 19) should return 2 because once the array has been sorted it will look like [3,5,20] and 19 is less than 20 (index 2) and greater than 5 (index 1).

function getIndexToIns(arr/*:any[]*/, num/*:number*/) {
  return arr.reduce((index, current) => num > current ? index + 1 : index, 0);
}

getIndexToIns([40, 60], 50);
#16. Caesars Cipher

One of the simplest and most widely known ciphers is a Caesar cipher, also known as a shift cipher. In a shift cipher the meanings of the letters are shifted by some set amount.

A common modern use is the ROT13 cipher, where the values of the letters are shifted by 13 places. Thus "A" ? "N", "B" ? "O" and so on.

Write a function which takes a ROT13 encoded string as input and returns a decoded string.

All letters will be uppercase. Do not transform any non-alphabetic character (i.e. spaces, punctuation), but do pass them on.

function rot13(str/*:string*/) {
  const key = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  let decode = "";
  for (let i = 0; i < str.length; i++) {
    const index = key.indexOf(str.charAt(i));
    if (index < 0) decode += str.charAt(i);
    else decode += key.charAt((index + 13) % 26);
  }
  return decode;
}

rot13("SERR PBQR PNZC");

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://m.specialneedsforspecialkids.com/yun/89400.html

相關(guān)文章

  • 求職準(zhǔn)備 - 收藏集 - 掘金

    摘要:一基礎(chǔ)接口的意義百度規(guī)范擴(kuò)展回調(diào)抽象類(lèi)的意義想不想通過(guò)一線(xiàn)互聯(lián)網(wǎng)公司面試文檔整理為電子書(shū)掘金簡(jiǎn)介谷歌求職記我花了八個(gè)月準(zhǔn)備谷歌面試掘金原文鏈接翻譯者 【面試寶典】從對(duì)象深入分析 Java 中實(shí)例變量和類(lèi)變量的區(qū)別 - 掘金原創(chuàng)文章,轉(zhuǎn)載請(qǐng)務(wù)必保留原出處為:http://www.54tianzhisheng.cn/... , 歡迎訪(fǎng)問(wèn)我的站點(diǎn),閱讀更多有深度的文章。 實(shí)例變量 和 類(lèi)變量...

    cuieney 評(píng)論0 收藏0
  • 2017-08-16 前端日?qǐng)?bào)

    摘要:前端日?qǐng)?bào)精選理解的專(zhuān)題之偏函數(shù)譯理解事件驅(qū)動(dòng)機(jī)制游戲開(kāi)發(fā)前端面試中的常見(jiàn)的算法問(wèn)題發(fā)布中文前端頁(yè)面?zhèn)鲄⑸袏y產(chǎn)品技術(shù)刊讀基礎(chǔ)系列二之實(shí)現(xiàn)大轉(zhuǎn)盤(pán)抽獎(jiǎng)掘金指南眾成翻譯編程插入排序眾成翻譯源碼講解函數(shù)技術(shù)風(fēng)暴初體驗(yàn)個(gè)人文 2017-08-16 前端日?qǐng)?bào) 精選 理解 JavaScript 的 async/awaitJavaScript專(zhuān)題之偏函數(shù)[譯]理解 Node.js 事件驅(qū)動(dòng)機(jī)制Pokem...

    graf 評(píng)論0 收藏0
  • FreeCodeCamp基礎(chǔ)算法題答案解析

    摘要:總結(jié)下基礎(chǔ)算法題先把字符串轉(zhuǎn)化成數(shù)組,再借助數(shù)組的方法翻轉(zhuǎn)數(shù)組順序,最后把數(shù)組轉(zhuǎn)化成字符串。檢查一個(gè)字符串是否以指定的字符串結(jié)尾。刪除數(shù)組中的所有假值。加密算法的核心是前個(gè)字母值加,后個(gè)字母值減從字母表重新回滾。 總結(jié)下FreeCodeCamp基礎(chǔ)算法題: Reverse a String 先把字符串轉(zhuǎn)化成數(shù)組,再借助數(shù)組的reverse方法翻轉(zhuǎn)數(shù)組順序,最后把數(shù)組轉(zhuǎn)化成字符串。 fu...

    sihai 評(píng)論0 收藏0
  • 16初級(jí)腳本算法,你要挑戰(zhàn)一下嗎?

    摘要:設(shè)置首字母大寫(xiě)算法挑戰(zhàn)返回一個(gè)字符串確保字符串的每個(gè)單詞首字母都大寫(xiě),其余部分小寫(xiě)。確認(rèn)末尾字符算法檢查一個(gè)字符串是否以指定的字符串結(jié)尾。刪除數(shù)組中特定值算法挑戰(zhàn)刪除數(shù)組中的所有的假值。 在w3cschool上看到了這些初級(jí)算法題目,自己先嘗試做了一下,不會(huì)的也會(huì)查看一下別人的借鑒一下思路,更多的幫助自己熟悉字符串和數(shù)組方法的應(yīng)用.如果您有更好的解法,可以指出來(lái)還有中級(jí)算法題目和后面的...

    kumfo 評(píng)論0 收藏0
  • 在線(xiàn)編程練習(xí)實(shí)踐網(wǎng)站

    摘要:在此收集一些自己遇到的一些在線(xiàn)練習(xí)的網(wǎng)站,當(dāng)然大部分是。建議邊學(xué)習(xí)邊編程,學(xué)習(xí)編程是不能光看不實(shí)踐的。國(guó)外的一個(gè)練習(xí)網(wǎng)站,有,也有,每種語(yǔ)言都有自己的道場(chǎng),每個(gè)用戶(hù)都有不同的等級(jí),刷題提高等級(jí),也可以插卡別人優(yōu)秀的解決方案。 在學(xué)習(xí)的過(guò)程中會(huì)發(fā)現(xiàn)很多知識(shí)點(diǎn)如果不在工作中運(yùn)用或者手寫(xiě)帶驗(yàn)證的話(huà),很容易忘記。任何技能的掌握都是需要不斷練習(xí)的。在此收集一些自己遇到的一些在線(xiàn)練習(xí)的網(wǎng)站,當(dāng)然大...

    huhud 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<