Pfungwa huru pano ndeyekudzosera kumashure tambo. Kana "reverse" tambo yakanyatsofanana neyokutanga, saka takagamuchira palindrome uye basa rinofanira kudzoka rechokwadi. Kana zvisina kudaro, nhema.
chisarudzo
Heino kodhi inogadzirisa iyo 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('')
}
Danho rekutanga nderekushandura mavara ari mutambo yekupinza kuita mavara madiki. Ichi chivimbiso chekuti chirongwa chichafananidza mavara ivo pachavo, uye kwete nyaya kana chimwe chinhu.
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)
}
}
}
// 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
}
Teerera kune iko kushandisa Object.keys() muchidimbu chiri pamusoro. Iyi nzira inodzosa rondedzero ine mazita kana makiyi nenzira imwechete yaanoonekwa muchinhu. Muchiitiko ichi, mutsara uchave wakadai:
['f', 'i', 'n', 'd', 'e', 'r']
Nenzira iyi tinowana zvimiro zvechinhu pasina kuita bhurawuti yakawanda. Mune dambudziko, unogona kushandisa nzira iyi ne .length property kuti uone kana tambo dzose dzine nhamba yakafanana yezvinyorwa - ichi chinhu chakakosha cheanagrams.
Tsvaga mavhawero
Basa rakareruka rinowanzouya mubvunzurudzo.
Stage
Unoda kunyora basa rinotora tambo senharo uye kudzorera nhamba yemavhawero ari mutambo.
Mavhawero anoti “a”, “e”, “i”, “o”, “u”.
Iwe unofanirwa kunyora basa rinodzorera nth rekodhi mune imwe nhevedzano, ine n iri iyo nhamba inopfuudzwa senharo kune basa.
fibonacci(3) // —> 2
Iri basa rinosanganisira kufamba nemuchiuno nhamba yenguva dzakatsanangurwa mugakava, kudzorera kukosha panzvimbo yakakodzera. Iyi nzira yekuisa dambudziko inoda kushandiswa kwezvishwe. Kana ukashandisa recursion pachinzvimbo, zvinogona kufadza mubvunzurudzo uye kukupa mamwe mashoma mapoinzi.
chisarudzo
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]
}
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)
}
Tinoramba tichidaidza fibonacci(), tichipfuura nhamba diki nediki senharo. Isu tinomira apo nharo yakapfuura iri 0 kana 1.
mhedziso
Zvingangodaro, iwe wakatosangana nechero yeaya mabasa kana iwe wakabvunzurudzwa kune yepamberi kana JavaScript yekuvandudza basa (kunyanya kana iri pajunior level). Asi kana usati wasangana nazvo, zvinogona kubatsira mune ramangwana - zvirinani kusimudzira.