5 yam kev xam phaj JavaScript ua haujlwm: tsom xam thiab kev daws teeb meem

5 yam kev xam phaj JavaScript ua haujlwm: tsom xam thiab kev daws teeb meem

Los ntawm tus txhais lus: luam tawm ib tsab xov xwm rau koj Maria Antonietta Perna, uas tham txog cov haujlwm JavaScript, feem ntau muab rau cov neeg thov tsim tawm thaum sib tham. Tsab ntawv yuav muaj txiaj ntsig, ua ntej ntawm tag nrho, rau cov programmers tshiab.

Kev xam phaj ntawm cov tuam txhab thev naus laus zis tau ntev los tham txog lub nroog. Qhov no yuav tsum tsis txhob xav tsis thoob - ua tiav kev sib tham ua tiav muab sijhawm rau koj kom tau txais txoj haujlwm zoo. Tab sis qhov no tsis yog yooj yim, txij li cov teeb meem nyuaj feem ntau yuav tsum tau daws.

Ntxiv mus, feem ntau, feem ntau ntawm cov haujlwm no tsis cuam tshuam txog kev ua haujlwm uas tus neeg thov yuav ua, tab sis lawv tseem yuav tsum tau daws. Qee lub sij hawm koj yuav tsum ua nws ntawm lub rooj tsavxwm, tsis tas kuaj nrog Google lossis lwm qhov chaw. Yog lawm, qhov xwm txheej no maj mam hloov, thiab qee lub tuam txhab tau tso tseg cov kev xam phaj no, tab sis ntau tus tswv haujlwm tseem ua raws li cov kev cai no. Kab lus no tau mob siab rau kev tshuaj xyuas ntawm cov haujlwm JavaScript uas feem ntau siv los ua haujlwm rau cov neeg nrhiav haujlwm.

Peb nco qab: rau txhua tus neeg nyeem Habr - 10 ruble luv nqi thaum tso npe rau hauv ib chav kawm Skillbox siv Habr promo code.

Skillbox pom zoo: Cov chav kawm siv tau "Mobile Developer PRO".

Qhov tseem ceeb yog kom npaj txhij rau koj qhov kev xam phaj.

Yog lawm, ua ntej peb pib saib cov haujlwm, cia peb saib qee cov lus qhia txog kev npaj kev xam phaj.

Qhov tseem ceeb tshaj plaws yog npaj ua ntej. Kuaj seb koj nco qab zoo npaum li cas algorithms thiab cov qauv ntaub ntawv, thiab txhim kho koj txoj kev paub hauv cov cheeb tsam uas koj tsis paub dhau. Muaj ntau lub vev xaib online uas tuaj yeem pab koj npaj rau kev xam phaj. Peb qhia geeksforgeeks, Puam, Interviewing.io и CodeSignal.

Nws tsim nyog kawm los hais qhov kev txiav txim siab nrov nrov. Nws raug nquahu kom qhia rau cov neeg thov txog qhov koj ua, thiab tsis yog sau rau ntawm lub rooj tsavxwm (lossis ntaus lej rau hauv lub computer, kuj ntsiag to). Txoj kev no, yog tias koj ua yuam kev hauv txoj cai, tab sis qhov kev daws teeb meem feem ntau yog qhov tseeb, koj tuaj yeem ua rau koj txoj hauv kev ua tiav.

Koj yuav tsum to taub qhov teeb meem ua ntej koj pib daws nws. Qee zaum, koj tuaj yeem nkag siab txog ib txoj haujlwm superficially thiab tom qab ntawd mus rau txoj kev tsis ncaj ncees lawm. Tej zaum nws yuav tsim nyog nug ob peb lo lus nug kom meej rau tus neeg xam phaj.

Koj yuav tsum xyaum sau cov lej ntawm tes, tsis yog hauv PC. Nws tshwm sim hais tias thaum sib tham tus neeg thov kev pab tau muab ib tug cim thiab ib tug whiteboard, qhov twg tsis muaj cov lus qhia los yog tsis siv neeg formatting. Thaum nrhiav kev daws teeb meem, nws tsim nyog sau koj cov cai ntawm ib daim ntawv lossis ncaj qha rau ntawm lub rooj tsavxwm. Yog tias koj khaws txhua yam hauv koj lub taub hau, koj yuav hnov ​​​​qab qee yam tseem ceeb.

Template cov haujlwm hauv JavaScript

Qee qhov haujlwm no tej zaum twb paub koj lawm. Koj tau muaj kev xam phaj qhov twg koj yuav tsum tau daws qee yam zoo sib xws, lossis xyaum ua lawv thaum kawm JavaScript. Zoo, tam sim no nws yog lub sijhawm los daws lawv dua, thiab nrog cov lus piav qhia ntxaws txog cov txheej txheem.

Palindrome

Lub palindrome yog ib lo lus, kab lus los yog ib ntus ntawm cov cim uas tau nyeem raws nraim tib yam ob qho tib si nyob rau hauv cov lus qhia ib txwm thiab nyob rau hauv qhov opposite direction. Piv txwv li, "Anna" yog ib qho palindrome, tab sis "lub rooj" thiab "John" tsis yog.

Staging

Muab ib txoj hlua; koj yuav tsum tau sau ib txoj haujlwm uas tso cai rau koj rov qab muaj tseeb yog tias txoj hlua yog palindrome, thiab tsis yog. Hauv qhov no, koj yuav tsum coj mus rau hauv tus account qhov chaw thiab cov cim cim.

palindrome('racecar') === tseeb
palindrome('table') === cuav

Cia peb txheeb xyuas txoj haujlwm

Lub tswv yim tseem ceeb ntawm no yog rov qab txoj hlua. Yog tias txoj hlua "rov qab" zoo ib yam nkaus rau tus thawj, ces peb tau txais ib qho palindrome thiab cov haujlwm yuav tsum rov qab muaj tseeb. Yog tsis yog, cuav.

kev txiav txim siab

Ntawm no yog cov cai uas daws cov palindrome.

const palindrome = str => {
  // turn the string to lowercase
  str = str.toLowerCase()
  // reverse input string and return the result of the
  // comparisong
  return str === str.split('').reverse().join('')
}

Thawj kauj ruam yog los hloov cov cim hauv txoj hlua nkag mus rau tus lej qis. Qhov no yog qhov lav tias qhov kev zov me nyuam yuav sib piv cov cim lawv tus kheej, thiab tsis yog rooj plaub lossis lwm yam.

Cov kauj ruam thib ob yog rov qab txoj kab. Qhov no tsis yooj yim ua: koj yuav tsum hloov nws mus rau hauv ib qho array siv .split() method (String library). Tom qab ntawd peb thim rov qab cov array siv .reverse() (Array qiv). Cov kauj ruam kawg yog los hloov cov array rov qab mus rau ib txoj hlua siv .join() (Array qiv).

Tam sim no txhua yam koj yuav tsum tau ua yog sib piv cov hlua "rov qab" nrog cov hlua qub, rov qab cov txiaj ntsig tseeb lossis cuav.

FizzBuzz

Ib txoj haujlwm nrov tshaj plaws hauv kev xam phaj.

Staging

Koj yuav tsum tau sau ib txoj haujlwm uas luam tawm cov lej ntawm 1 txog n mus rau lub console, qhov twg n yog tus lej uas muaj nuj nqi siv raws li qhov ntsuas, nrog rau cov xwm txheej hauv qab no:

  • tso zis fizz es tsis txhob ntau ntawm 3;
  • tso zis buzz es tsis txhob cov lej uas yog ntau ntawm 5;
  • fizzbuzz tso zis es tsis txhob ntawm cov lej uas muaj ntau yam ntawm 3 thiab 5.

Piv Txwv:

Fizzbuzz (5)

tshwm sim

// 1 ib
// 2 ib
// faus
// 4 ib
// lus

Cia peb txheeb xyuas txoj haujlwm

Qhov tseem ceeb ntawm no yog ib txoj hauv kev los nrhiav ntau yam siv JavaScript. Nws tuaj yeem siv tau siv tus neeg teb xov tooj modulus lossis qhov seem - %, uas tso cai rau koj los qhia qhov seem thaum faib ob tus lej. Yog tias qhov seem yog 0, nws txhais tau tias thawj tus lej yog ntau qhov thib ob.

12% 5 // 2 -> 12 tsis yog ntau ntawm 5
12% 3 // 0 -> 12 yog ntau yam ntawm 3

Yog li, yog tias koj faib 12 rau 5, koj tau 2 nrog qhov seem ntawm 2. Yog tias koj faib 12 los ntawm 3, koj tau txais 4 nrog qhov seem ntawm 0. Hauv thawj kis, 12 tsis yog qhov ntau ntawm 5, hauv qhov thib ob. , 12 yog ntau yam ntawm 3.

kev txiav txim siab

Qhov kev daws teeb meem zoo tshaj plaws yuav yog cov cai hauv qab no:

const fizzBuzz = num => {
  for(let i = 1; i <= num; i++) {
    // check if the number is a multiple of 3 and 5
    if(i % 3 === 0 && i % 5 === 0) {
      console.log('fizzbuzz')
    } // check if the number is a multiple of 3
      else if(i % 3 === 0) {
      console.log('fizz')
    } // check if the number is a multiple of 5
      else if(i % 5 === 0) {
      console.log('buzz')
    } else {
      console.log(i)
    }
  }
}

Lub luag haujlwm ua cov kev kuaj xyuas tsim nyog uas siv cov lus teev tseg thiab tsim cov txiaj ntsig xav tau los ntawm tus neeg siv. Hauv qhov teeb meem, nws tsim nyog them sai sai rau qhov kev txiav txim ntawm yog ... lwm nqe lus: pib nrog ob qhov xwm txheej (&&) thiab xaus nrog rooj plaub uas ntau tus lej nrhiav tsis tau. Yog li ntawd, peb npog tag nrho cov kev xaiv.

Anagram

Qhov no yog lub npe rau ib lo lus uas muaj tag nrho cov tsiaj ntawv ntawm lwm lo lus nyob rau hauv tib lub xov tooj, tab sis nyob rau hauv ib tug txawv kev txiav txim.

Staging

Peb yuav tsum tau sau ib txoj haujlwm uas kuaj xyuas seb ob txoj hlua puas yog anagrams, thiab rooj plaub tsis muaj teeb meem. Tsuas suav cov cim; qhov chaw lossis cov cim cim tsis raug suav nrog.

anagram('finder', 'Friend') —> tseeb
anagram('nyob zoo', 'bye') —> cuav

Cia peb txheeb xyuas txoj haujlwm

Qhov tseem ceeb uas yuav tau xav txog ntawm no yog tias koj yuav tsum kuaj xyuas txhua tsab ntawv hauv ob kab lus nkag thiab lawv tus lej hauv txhua kab.

finder —> f: 1 phooj ywg —> f: 1
ib: 1r:1
n:1 ib:
d :1e:1
ib: 1n:
r:1d 1

Txhawm rau khaws cov ntaub ntawv anagram, koj yuav tsum xaiv cov qauv xws li JavaScript khoom siv. Qhov tseem ceeb nyob rau hauv cov ntaub ntawv no yog tus cwj pwm ntawm tsab ntawv, tus nqi yog tus naj npawb ntawm nws repetitions nyob rau hauv cov kab tam sim no.

Muaj lwm yam xwm txheej:

  • Koj yuav tsum xyuas kom meej tias cov ntaub ntawv ntawm cov ntawv tsis raug coj mus rau hauv tus account thaum sib piv. Peb tsuas hloov ob txoj hlua mus rau sab saud lossis sab saud.
  • Peb tshem tawm tag nrho cov tsis yog tus cwj pwm los ntawm kev sib piv. Qhov zoo tshaj plaws los ua haujlwm nrog cov lus qhia tsis tu ncua.

kev txiav txim siab

// helper function that builds the
// object to store the data
const buildCharObject = str => {
  const charObj = {}
  for(let char of str.replace(/[^w]/g).toLowerCase()) {
    // if the object has already a key value pair
    // equal to the value being looped over,
    // increase the value by 1, otherwise add
    // the letter being looped over as key and 1 as its value
    charObj[char] = charObj[char] + 1 || 1
  }
  return charObj
}
 
// main function
const anagram = (strA, strB) => {
  // build the object that holds strA data
  const aCharObject = buildCharObject(strA)
  // build the object that holds strB data
  const bCharObject = buildCharObject(strB)
 
  // compare number of keys in the two objects
  // (anagrams must have the same number of letters)
  if(Object.keys(aCharObject).length !== Object.keys(bCharObject).length) {
    return false
  }
  // if both objects have the same number of keys
  // we can be sure that at least both strings
  // have the same number of characters
  // now we can compare the two objects to see if both
  // have the same letters in the same amount
  for(let char in aCharObject) {
    if(aCharObject[char] !== bCharObject[char]) {
      return false
    }
  }
  // if both the above checks succeed,
  // you have an anagram: return true
  return true
}

Them sai sai rau kev siv Object.keys() nyob rau hauv lub snippet saum toj no. Txoj kev no xa rov qab ib qho array uas muaj cov npe lossis cov yuam sij hauv tib qhov kev txiav txim uas lawv tshwm sim hauv cov khoom. Hauv qhov no, array yuav zoo li no:

['f', 'i', 'n', 'd', 'e', ​​'r']

Txoj kev no peb tau txais cov khoom ntawm cov khoom yam tsis tas yuav ua ntau lub voj voog. Hauv ib qho teeb meem, koj tuaj yeem siv txoj kev no nrog cov cuab yeej .length los xyuas seb ob txoj hlua puas muaj cov cim tib yam - qhov no yog qhov tseem ceeb ntawm cov anagrams.

Nrhiav cov lus

Ib txoj haujlwm yooj yim uas feem ntau tuaj hauv kev xam phaj.

Staging

Koj yuav tsum tau sau ib txoj haujlwm uas siv txoj hlua ua ib qho kev sib cav thiab xa rov qab tus lej ntawm cov lus uas muaj nyob hauv txoj hlua.
Cov vowels yog "a", "e", "i", "o", "u".

Piv Txwv:

findVowels('nyob zoo') //—> 2
findVowels('vim') // —> 0

kev txiav txim siab

Nov yog qhov kev xaiv yooj yim tshaj plaws:

const findVowels = str => {
  let count = 0
  const vowels = ['a', 'e', 'i', 'o', 'u']
  for(let char of str.toLowerCase()) {
    if(vowels.includes(char)) {
      count++
    }
  }
  return count
}

Nws yog ib qho tseem ceeb uas yuav tau xyuam xim rau kev siv .includes() txoj kev. Nws muaj rau ob txoj hlua thiab arrays. Nws yuav tsum tau siv los txiav txim seb ib qho array puas muaj qee yam nqi. Txoj kev no rov qab muaj tseeb yog tias cov array muaj cov nqi teev tseg, thiab tsis tseeb yog tias nws tsis ua.

Muaj ib txoj kev luv luv rau qhov teeb meem:

const findVowels = str => {
  const matched = str.match(/[aeiou]/gi)
  return matched ? matches.length : 0
}

Qhov no siv .match() txoj kev, uas tso cai rau koj los siv kev tshawb fawb zoo. Yog hais tias ib qho kev qhia tsis tu ncua raws li ib qho kev sib cav pom nyob rau hauv cov hlua uas tau teev tseg, ces tus nqi rov qab yog ib qho kev sib txuam ntawm cov cim. Zoo, yog tias tsis muaj qhov sib tw, ces .match() rov null.

fibonacci ua

Ib txoj haujlwm classic uas tuaj yeem pom hauv kev xam phaj ntawm ntau qib. Nws tsim nyog nco qab tias Fibonacci ib ntus yog cov lej uas txhua tus tom ntej yog cov lej ntawm ob qho dhau los. Yog li, thawj kaum tus lej zoo li no: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34.

Staging

Koj yuav tsum tau sau ib txoj haujlwm uas xa rov qab cov ntaub ntawv nth hauv ib ntu, nrog n yog tus lej uas tau dhau los ua qhov kev sib cav rau txoj haujlwm.

fibonacci(3) //—> 2

Txoj haujlwm no suav nrog kev taug kev los ntawm lub voj voog tus naj npawb ntawm lub sijhawm teev hauv qhov kev sib cav, rov qab tus nqi ntawm txoj haujlwm tsim nyog. Txoj kev ntawm qhov teeb meem no yuav tsum tau siv cov loops. Yog tias koj siv recursion es tsis txhob, nws yuav thov tus neeg xam phaj thiab muab qee cov ntsiab lus ntxiv rau koj.

kev txiav txim siab

const fibonacci = num => {
  // store the Fibonacci sequence you're going
  // to generate inside an array and
  // initialize the array with the first two
  // numbers of the sequence
  const result = [0, 1]
 
  for(let i = 2; i <= num; i++) {
    // push the sum of the two numbers
    // preceding the position of i in the result array
    // at the end of the result array
    const prevNum1 = result[i - 1]
    const prevNum2 = result[i - 2]
    result.push(prevNum1 + prevNum2)
  }
  // return the last value in the result array
  return result[num]
}

Hauv cov txiaj ntsig array, thawj ob tus lej muaj nyob rau hauv koob vim tias txhua qhov nkag hauv qhov sib lawv liag yog cov lej ntawm ob tus lej dhau los. Thaum pib, tsis muaj ob tus lej uas tuaj yeem coj mus rau tus lej tom ntej, yog li lub voj tsis tuaj yeem tsim lawv tau. Tab sis, raws li peb paub, thawj ob tus lej yeej ib txwm yog 0 thiab 1. Yog li ntawd, koj tuaj yeem pib qhov kev tshwm sim array manually.

Raws li rau recursion, txhua yam yog yooj yim thiab ntau nyuaj nyob rau tib lub sij hawm:

const fibonacci = num => {
  // if num is either 0 or 1 return num
  if(num < 2) {
    return num
  }
  // recursion here
  return fibonacci(num - 1) + fibonacci(num - 2)
}

Peb pheej hu fibonacci(), hla tus lej me thiab me me raws li kev sib cav. Peb nres thaum qhov kev sib cav dhau los yog 0 lossis 1.

xaus

Feem ntau, koj twb tau ntsib ib qho ntawm cov haujlwm no yog tias koj tau xam phaj rau pem hauv ntej lossis JavaScript tus tsim txoj haujlwm (tshwj xeeb tshaj yog tias nws yog qib qis). Tab sis yog tias koj tsis tau hla lawv, lawv yuav muaj txiaj ntsig yav tom ntej - tsawg kawg rau kev txhim kho dav dav.

Skillbox pom zoo:

Tau qhov twg los: www.hab.com

Ntxiv ib saib