goit-js-hw-02 was createdtask-номер_завдання.js Use <script type="module"> to close the task code in a separate scope and avoid conflicts of identifier names. Write logItems(array) feature that receives an array and uses a for loop, which will be displayed for each element of the array in the format console [номер елемента] - [значення елемента] .
The numbering should start with 1 . For example, for the first element of the array ['Mango', 'Poly', 'Ajax'] with index 0 will be deduced '1 - Mango' , and for index 2 will display '3 - Ajax' .
const logItems = function ( array ) {
// твій код
} ;
/*
* Виклич функції для перевірки працездатності твоєї реалізації.
*/
logItems ( [ "Mango" , "Poly" , "Ajax" , "Lux" , "Jay" , "Kong" ] ) ;
logItems ( [ 5 , 10 , 15 , 20 , 25 , 30 , 35 , 40 , 45 , 50 ] ) ; Write a script of the cost of engraving jewelry. To do this, create a calculateEngravingPrice(message, pricePerWord) function (only words and gaps) and the price of engraving of one word, and returns the engraving of all words in a line.
const calculateEngravingPrice = function ( message , pricePerWord ) {
// твій код
} ;
/*
* Виклич функції для перевірки працездатності твоєї реалізації.
*/
console . log (
calculateEngravingPrice (
"Proin sociis natoque et magnis parturient montes mus" ,
10
)
) ; // 80
console . log (
calculateEngravingPrice (
"Proin sociis natoque et magnis parturient montes mus" ,
20
)
) ; // 160
console . log (
calculateEngravingPrice ( "Donec orci lectus aliquam est magnis" , 40 )
) ; // 240
console . log (
calculateEngravingPrice ( "Donec orci lectus aliquam est magnis" , 20 )
) ; // 120 Write the findLongestWord(string) feature that accepts an arbitrary line (only words and gaps in the line) and returns the longest word in this line.
const findLongestWord = function ( string ) {
// твій код
} ;
/*
* Виклич функції для перевірки працездатності твоєї реалізації.
*/
console . log ( findLongestWord ( "The quick brown fox jumped over the lazy dog" ) ) ; // 'jumped'
console . log ( findLongestWord ( "Google do a roll" ) ) ; // 'Google'
console . log ( findLongestWord ( "May the force be with you" ) ) ; // 'force' Write formatString(string) feature that takes and format it if necessary.
40 символів , the function returns it in the initial form.40 символів , then the function trims the line up to 40 characters and adds three dots to the end of the line '...' , then returns the short version. const formatString = function ( string ) {
// твій код
} ;
/*
* Виклич функції для перевірки працездатності твоєї реалізації.
*/
console . log ( formatString ( "Curabitur ligula sapien, tincidunt non." ) ) ;
// повернеться оригінальний рядок
console . log ( formatString ( "Vestibulum facilisis, purus nec pulvinar iaculis." ) ) ;
// повернеться форматований рядок
console . log ( formatString ( "Curabitur ligula sapien." ) ) ;
// повернеться оригінальний рядок
console . log (
formatString (
"Nunc sed turpis. Curabitur a felis in nunc fringilla tristique."
)
) ;
// повернеться форматований рядок Write a checkForSpam(message) feature, hosting 1 message parameter. The function checks it for the content of the words spam and sale . If you find a forbidden word, then the function returns true if the prohibited words are not a function returns false . Words in a line may be in an arbitrary register.
const checkForSpam = function ( message ) {
// твій код
} ;
/*
* Викличи функції для перевірки працездатності твоєї реалізації.
*/
console . log ( checkForSpam ( "Latest technology news" ) ) ; // false
console . log ( checkForSpam ( "JavaScript weekly newsletter" ) ) ; // false
console . log ( checkForSpam ( "Get best sale offers now!" ) ) ; // true
console . log ( checkForSpam ( "[SPAM] How to earn fast money?" ) ) ; // trueWrite a script followed by functionality:
prompt . The introduction is stored in input variable and is added to the numbers numbers.Cancel in prompt .Cancel , if the array is not empty, it is necessary to calculate the sum of all the elements of the array and record it in a total variable. Use the for for or for...of . Then, in the console, remove the line 'Загальна сума чисел дорівнює [сума]' . ? Checking that the user has introduced the number, not an arbitrary set of characters, is not required. If you want, in the case of incorrect input, display alert with the text 'Було введено не число, попробуйте ще раз' , while the result of prompt is not required in an array of numbers, then again the user is asked to enter the number in prompt .
let input ;
const numbers = [ ] ;
let total = 0 ; There is an array logins with user logins. Write the script adding the login to logins array. The login that attached should:
logins arrayBreak the task on the subtitude with the help of functions.
Write isLoginValid(login) function in which the number of login parameter characters check and rotate true or false , depending on whether the parameter length falls into a given range of 4 to 16 characters inclusive.
Write the isLoginUnique(allLogins, login) , which accepts a list of all logins and logins, which is added as parameters and checks the presence of login in the Massve allLogins , returns true if such a login is not yet used and false if the login is already used.
Write addLogin(allLogins, login) feature:
isLoginValidaddLogin function and return the radical 'Помилка! Логін повинен бути від 4 до 16 символів'addLogin feature checks the uniqueness of login using isLoginUnique functionisLoginUnique returns false , then addLogin does not add a login to the massif and returns the line 'Такий логін уже використовується!'isLoginUnique turns true , addLogin adds a new login to logins and returns line 'Логін успішно доданий!'? The principle of a single responsibility of function - every function makes one thing. This allows you to exceed the code and change the logic of function in only one place without affecting the work of the program as a whole.
Predicate functions return only true or false . Such functions are called from is : isLoginUnique and isLoginValid in our case.
isLoginUnique only checks whether there is such a login in an array and returns true or false .isLoginValid only checks whether the valid login and returns true or false .addLogin is added or not added to the login. In this case, the conditions of addition use the results of the calls of other functions - isLoginUnique and isLoginValid . const logins = [ "Mango" , "robotGoogles" , "Poly" , "Aj4x1sBozz" , "qwerty123" ] ;
const isLoginValid = function ( login ) {
// твій код
} ;
const isLoginUnique = function ( allLogins , login ) {
// твій код
} ;
const addLogin = function ( allLogins , login ) {
// твій код
} ;
/*
* Виклич функції для перевірки працездатності твоєї реалізації.
*/
console . log ( addLogin ( logins , "Ajax" ) ) ; // 'Логін успішно доданий!'
console . log ( addLogin ( logins , "robotGoogles" ) ) ; // 'Такий логін вже використовується!'
console . log ( addLogin ( logins , "Zod" ) ) ; // 'Помилка! Логін повинен бути від 4 до 16 символів'
console . log ( addLogin ( logins , "jqueryisextremelyfast" ) ) ; // 'Помилка! Логін повинен бути від 4 до 16 символів'