The best thing you can do with JavaScript #javascript #programming

preview_player
Показать описание

Рекомендации по теме
Комментарии
Автор

All those years of evolving programming languages… Just to turn a sentence into 69…

roku_on_it
Автор

Now I'm gonna impress my interviewer

RealVenkatesh
Автор

“Whats your perfect first date?” The answer is 69. 😂 thanks brother

ninetyseven
Автор

Finally a tutorial that i actually can keep up.

lonelymge
Автор

I wasn't expecting to get a good laugh from this... nicely done 😂

Kelz_
Автор

When you search for Javascript Tutorial in wrong site:

HEXLANG
Автор

I really thought I was about to learn something truly profound.

krugerstan
Автор

I loved the way he looked at us with that smile 😅

nexcode_ir
Автор

This pattern in javascript is one of the reasons i fell in love with javascript

gabriellane
Автор

Fun fact most of those methods that are array specific decompress into individual loops. So there are 3 separate loops in this example whereas if you just split the string and did everything manually in a single for loop you reduce the total number of iterations.

TLDR; Doing this in a single for loop with if statements it’s more efficient when n is large

michaelaboah
Автор

I really expected this to be useful until the end. Well played

muddrox
Автор

For those wondering what's happening? Short explanation.

.map(char => parseInt(char)) would only return [NaN, NaN]

But with .map(parseInt) things are different.

Normally most of you guys have been using map and parseint by passing a single argument. like most requirements demand.

But there are more arguments, that .map passes for its callbacks.

map((element, index, array) => console.log(element, index, array))

and for praseInt(string, radix) where radix is the base number system. from 2 to 36

so basically what's happening here is .map((char, index) => parseInt(char, index))

justafreakable
Автор

const output =
"What's your perfect first date?"
.split("") // Step 1: Split the string into an array of characters
.map(parseInt) // Step 2: Attempt to convert each character into an integer (NaN for non-numeric chars)
.filter(a => a) // Step 3: Remove falsy values (removes NaN and 0)
.reduce((a, b) => a + b) // Step 4: Sum up all remaining numbers
.toString() // Step 5: Convert the sum to a string
.split("") // Step 6: Split the string into an array of characters
.reverse() // Step 7: Reverse the array
.join(""); // Step 8: Join the reversed characters into a final string

console.log(output); // Prints the final result

nileshsalunke
Автор

The issue stems from how parseInt behaves inside map(). parseInt takes two arguments: the string to convert and an optional radix (base). When used in map(), it receives the character and its index, leading to unexpected results.

For example, "f" (index 15) gets interpreted as parseInt("f", 15), where "f" is valid in base 16 (0-9, a-f), so it returns 15. However, many other characters don't make sense in their given index-based radix, returning NaN (or null in the alert output).

A safer approach would be:

javascript
Copy
Edit
const mappedArr = splitStr.map(char => parseInt(char, 10));
But that would still only work for numeric characters. Since your original intent seems to be extracting numbers, the behavior you observed is just a quirk of JavaScript.

nileshsalunke
Автор

The reason it does this is because .map passes both the value and index into parseInt (i.e. it's the same as `.map((value, index)=> parseInt(value, index))`). The second parameter of parseInt sets the base of the number in the string, so it's going parseInt("w", 0), parseInt("h", 1), parseInt("a", 2), etc.. Most of these are invalid numbers until the 2nd "e" in "perfect" which is at index 16 of the string, so parseInt("e", 16) returns 14 becausa "e" is 14 in base 16 (a.k.a. hexadecimal). As .map continues going through the string-array, the base increases, and more characters become valid numbers. The rest of the code removes the NaNs (not a number) and adds up the numbers.

LemonatorGP
Автор

I was going to share this to my student before I saw the end. Good one though

iMaxos
Автор

What's your perfect date?
At the end, split().reverse().join()☺️

balaganesharuna
Автор

2024: the year of dev influencers with mustaches making 69 jokes

TheRealCornPop
Автор

Nice. Works similarly well in Rust with chars iterator ;)

jamesnewman
Автор

I came expecting something and I wasn't disappointed

languagelearningexperience
join shbcf.ru