Swap values of 2 variables without using 3rd variable 🤔

Опубликовано: 18 Июнь 2024
на канале: Mayank Srivastava
1,333
32

Hey everyone! Today, let's learn a quick and easy way to swap two numbers in JavaScript without using a third variable.This is a very common interview question that is usually for entry level jobs. Normally, you'd use a temporary variable lets say c to swap them, but there's a much cooler way to do it without that extra variable taking up unnecessary space. So what we want to do here is that we will first store the sum of both a and b inside a,
then to assign the variable b the original value of a we now just need to subtract the value of b from the new value of a, which is the arithmetic equivalent of subtracting the value 3 from 1 +3 so we are just left with 1, which is the orignal value of a. Once this is done using the same principle we remove this new b value from a + b leaving us with 3 which can now be assigned to a.


На этой странице сайта вы можете посмотреть видео онлайн Swap values of 2 variables without using 3rd variable 🤔 длительностью часов минут секунд в хорошем качестве, которое загрузил пользователь Mayank Srivastava 18 Июнь 2024, поделитесь ссылкой с друзьями и знакомыми, на youtube это видео уже посмотрели 1,333 раз и оно понравилось 32 зрителям. Приятного просмотра!