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.
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:
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.
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.
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.