let d19 = ['Your', 'payment', 'method', 'will', 'automatically', 'be', 'charged', 'in', 'advance', 'every'];
let maxString = d19[0];
function f19() {
for (let i = 0; i <d19.length;i++)
if(d19[i]> maxString){
maxString = d19[i]
console.log(maxString)}
}
document.querySelector('.b-19').onclick = f19;
<section>
<p><b>Task 19.</b></p>
<p>Напишите фукнцию f19, которая выводит самую длинную строку maxString из массива d19 в out-19.</p>
<pre>d19 = ['Your','payment','method','will','automatically','be','charged','in','advance','every' ];</pre>
<button class="button-primary b-19">Task-19</button>
<div class="out-19"></div>
</section>
const longestStr = arr.reduce((max, n) => max.length > n.length ? max : n, '');
// или
const longestStr = arr.sort((a, b) => b.length - a.length)[0];
// или
const longestStr = arr.reduce((acc, n) => (acc[n.length] = n, acc), []).pop();
function max(data, key = n => n) {
const getVal = key instanceof Function ? key : n => n[key];
let result = null;
for (const n of data) {
const val = getVal(n);
if (!result || result[1] < val) {
result = [ n, val ];
}
}
return result?.[0];
}
const longestStr = max(arr, 'length');
let d19 = ['Your', 'payment', 'method', 'will', 'automatically', 'be', 'charged', 'in', 'advance', 'every'];
let maxString = '';
let out19 = document.querySelector('.out-19');
function f19() {
for (i = 0; i < d19.length; i++) {
if (d19[i].length > maxString.length) {
maxString = d19[i]
}
}
out19.innerHTML = maxString;
}
document.querySelector('.b-19').onclick = f19;