Javascript Challenges - Second Value

preview_player
Показать описание
Javascript Challenges - Second Value
COUPONS BELOW!!!!!!

REACT

JAVASCRIPT COURSE

BOOTSTRAP COURSE

IN DEPTH HTML AND CSS

RESPONSIVE WEBISTES

JQUERY COURSE

FLEXBOX COURSE

GRID COURSE

RESPONSIVE COFFEE SHOW WEBSITE

RESPONSIVE CAR DEALERSHIP

COUPONS BELOW!!!!!!

JAVASCRIPT COURSE

BOOTSTRAP COURSE

IN DEPTH HTML AND CSS

RESPONSIVE WEBISTES

JQUERY COURSE

FLEXBOX COURSE

GRID COURSE

Products I Use:

Books I Recommend:

Disclosure: This video is not sponsored. Some links above are affiliate links, and l may earn a small commission from any purchases at no additional cost to you. Thank you for supporting my channel!
Рекомендации по теме
Комментарии
Автор

Challenges are just wonderful!! why people not like this!

MahadyHasan
Автор

I like the use of set. It didn't immediately spring to mind.

jdevcast
Автор

Didn't know for new Set(), thanks...

milannis
Автор

const secondVal = (arr) => {
let sorted = [...new Set(arr.sort((a, b) => a - b))];
switch (sorted.length) {
case 1:
case 2:
return sorted.join(", ");
break;
default:
return [...new Set([sorted[1], sorted[sorted.length-2]])].join(", ");
break;
};
};

abdelkarimabouzi
Автор

improvement:
if(values.length > 1){
return `${values[1]} ${values[values.length - 2]}
}

mominahmad
Автор

function secondLargeAndSmall(arr) {
if (arr.length == 0) {
return `array must need 2 elements`
} else if (arr.length == 1) {
return arr
}
else if (arr.length == 2) {
if (arr[0] < arr[1]) {
return [arr]
}
return [arr[1], arr[0]]
} else {
let min1 = Infinity
let min2 = Infinity
let max1 = -Infinity
let max2 = -Infinity
for (let val of arr) {
if (val < min1) {
min2 = min1
min1 = val
} else if (val < min2 && val != min1) {
min2 = val
}
if (val > max1) {
max2 = max1
max1 = val
} else if (val > max2 && val != max1) {
max2 = val
}
}
return [min2, max2]
}
}

nandakumart
Автор

function secondValue(arr)
{
let newArray = [...new Set(arr.sort((a, b)=> a - b))];

if(newArray.length <= 2)
return arr;

return [...new Set([newArray.at(1), newArray.at(-2)])]
}


console.log(secondValue([4, 2]));
console.log(secondValue([11, 44, 22]));
console.log(secondValue([3, 2, 88, 3, -11, 67, 7]));

felixpaniagua