From e4260f2bdd2c5f716a126edd3e05f7cbce7ff19d Mon Sep 17 00:00:00 2001 From: Roman Date: Sun, 29 Sep 2024 12:46:53 +0300 Subject: [PATCH] add hw7 task 1-2 --- hw6Task2.js | 26 ++++++++++++++++++++++++++ hw7Task1.js | 39 +++++++++++++++++++++++++++++++++++++++ hw7Task2.js | 44 +++++++++++++++++++++++++------------------- 3 files changed, 90 insertions(+), 19 deletions(-) create mode 100644 hw6Task2.js create mode 100644 hw7Task1.js diff --git a/hw6Task2.js b/hw6Task2.js new file mode 100644 index 0000000..aabf1b0 --- /dev/null +++ b/hw6Task2.js @@ -0,0 +1,26 @@ + + +const competitorPizzas = ['Peperoni', 'Caprichosa', 'Diablo', '4 cheeses', 'hawai'] +const myPizzas = ['peperoni', 'Caprichosa', 'Diablo', '4 cheeses'] + +const pizzaResult = [] + +for (let i = 0; i < competitorPizzas.length; i++) { + competitorPizzas[i] = competitorPizzas[i].toLowerCase() +} + +for (let i = 0; i < myPizzas.length; i++) { + myPizzas[i] = myPizzas[i].toLowerCase() +} + +for (const pizza of myPizzas) { + if (!competitorPizzas.includes(pizza)) { + pizzaResult.push(pizza) + } +} + +if (pizzaResult.length === 0) { + console.log(null) +} else { + console.log(pizzaResult) +} \ No newline at end of file diff --git a/hw7Task1.js b/hw7Task1.js new file mode 100644 index 0000000..fc25207 --- /dev/null +++ b/hw7Task1.js @@ -0,0 +1,39 @@ + +// function showArgs(...arguments) { +// const result = [] +// for (const arg of arguments){ +// result.push(...arg) +// } +// console.log(result) +// } + +// showArgs([1, 2],[3, 4], [5, 6]) + +function derive(text) { + let result = text.replaceAll(' ', '_') + let finalResult = '' + + for (let i = 0; i < result.length; i++) { + if (result[i] === '_') { + finalResult += '_' + if (i + 1 < result.length) { + finalResult += result[i + 1].toUpperCase() + i++ + } + } else { + finalResult += result[i] + } + } + + return finalResult[0].toLowerCase() + finalResult.slice(1) +} + +console.log(derive('I am super Engineer')) + + +function fibonacci(n) { + if (n <= 1) return n; + return fibonacci(n - 1) + fibonacci(n - 2); +} + +console.log(fibonacci(8)) \ No newline at end of file diff --git a/hw7Task2.js b/hw7Task2.js index aabf1b0..4acc760 100644 --- a/hw7Task2.js +++ b/hw7Task2.js @@ -1,26 +1,32 @@ +function isPalindrome(word) { - -const competitorPizzas = ['Peperoni', 'Caprichosa', 'Diablo', '4 cheeses', 'hawai'] -const myPizzas = ['peperoni', 'Caprichosa', 'Diablo', '4 cheeses'] - -const pizzaResult = [] - -for (let i = 0; i < competitorPizzas.length; i++) { - competitorPizzas[i] = competitorPizzas[i].toLowerCase() + const normalizedWord = word.toLowerCase(); + + const reversedWord = normalizedWord.split('').reverse().join(''); + + return normalizedWord === reversedWord; } -for (let i = 0; i < myPizzas.length; i++) { - myPizzas[i] = myPizzas[i].toLowerCase() -} +console.log(isPalindrome('madam')) +console.log(isPalindrome('hello')) +console.log(isPalindrome('Racecar')) + +function longestWords(sentence) { + const words = sentence.split(' ') + let maxLength = 0; + let longestWords = []; -for (const pizza of myPizzas) { - if (!competitorPizzas.includes(pizza)) { - pizzaResult.push(pizza) + for (const word of words) { + if (word.length > maxLength) { + maxLength = word.length + longestWords = [word] + } else if (word.length === maxLength) { + longestWords.push(word) + } } + + return longestWords; } -if (pizzaResult.length === 0) { - console.log(null) -} else { - console.log(pizzaResult) -} \ No newline at end of file +console.log(longestWords('I am super engineer and coder')) +console.log(longestWords('I love coding and programming and depgramming')) \ No newline at end of file