You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Lecture 6 - JavaScript Array and Object Deep Dive (Time: 2:03:56) #56
In this lecture, Nayem vai with an array like const number = [1,2,3,4,NaN,false,'',5,7,8] For converting an array to a truthy string and making a custom array with string concatenation like [ 1, 2, 3, 4, 5, 7, 8 ] The following code is
Activity
MOHAMMADmiZAN commentedon Feb 19, 2023
const number = [1,2,3,4,NaN,false,''];
const truthyValue = number.reduce((acc,cur, index) => {
if (index === 0) acc += '[ ';
if (cur){
acc += cur.toString() + (
${index < number.length - 1 ? ', ' : ''});}
if (cur && !number.slice(index+1).some(Boolean)) acc+=' ]';
return acc;
}, '');
console.log(truthyValue); // Output: [ 1, 2, 3, 4 ]
nuruddin-bin-hoda commentedon Jun 1, 2023
Try this algorithm
const arr = [1, 2, 3, 4, NaN, false, ""];
let i, j = 0;
for (i = 0; i < arr.length; i++) {
if (typeof arr[i] === "number" && !isNaN(arr[i])) {
arr[j++] = arr[i];
}
}
arr.length -= i - j;