Тёмный

Check if a String is a palindrome with JavaScript Tutorial 

Code With Bubb
Подписаться 57 тыс.
Просмотров 21 тыс.
50% 1

In this tutorial you’ll learn how to check if a string is a palindrome using JavaScript. Get my free 32 page eBook of JavaScript HowTos 👉 bit.ly/2ThXPL3
Don’t forget to subscribe to the Junior Developer Central channel for more videos and tutorials!
So there are a couple of ways to check is a string is a palindrome in JavaScript and we’ll first look at what seems to be the most logical way of doing it which is using a for loop.
In the tutorial i’ll take you through the process of looping through a string to check if each character at opposite ends of the string are the same.
This process is a pretty simple way to check a string (or number) is a palindrome or not in JavaScript however we’ll look at one problem with this which is if the string has unexpected spaces, commas or other symbols. By definition, as long as the actual letters match up (without any of the symbols) then the string is technically still a palindrome so we should accept it.
So we need a way to remove these characters that we don’t want to check for in our palindrome with JavaScript. You’ll learn a simple way to do this using a Regular Expression.
Once you’ve learnt a way to check for a palindrome using a for loop, we’ll look at some ways of doing it with the javascript reverse function and also a bit of a more esoteric method using a filter function to remove items from the array if they don’t match the end of the string.
Other videos in the JavaScript Snippets series:
Find the largest number in an array JavaScript Tutorial: • Find the largest numbe...
How to find the longest word in a String with JavaScript: • How to find the longes...
How to reverse a String in JavaScript Tutorial: • How to reverse a Strin...
Javascript String Length: How to determine the size of a string: • Javascript String Leng...
JavaScript join method: How to merge arrays into one value: • JavaScript join method...
JavaScript How To Remove An Item From Array Tutorial: • JavaScript How To Remo...
Javascript How To Convert String To Number Tutorial: • Javascript How To Conv...
JavaScript Create HTML Element: How to dynamically add tags to your pages: • JavaScript Create HTML...
JavaScript String Contains: How to check a string exists in another: • JavaScript String Cont...
Link JavaScript to HTML: How to run your JavaScript code in the browser: • Link JavaScript to HTM...
JavaScript Copy Array: How to make an exact copy of an array in JavaScript: • JavaScript Copy Array:...
JavaScript Capitalize First Letter: How to make strings and arrays sentence case: • JavaScript Capitalize ...
Javascript Print To Console Tutorial: Different ways to output data to the console: • Javascript Print To Co... Channel Handle @codebubb

Опубликовано:

 

16 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 29   
@codewithbubb
@codewithbubb 5 лет назад
Also check out how to find the largest number in an array: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-fOFSmCjvcFY.html
@georgemichael9481
@georgemichael9481 3 года назад
I guess it's kind of off topic but does anybody know a good site to watch new movies online ?
@titanachilles7627
@titanachilles7627 3 года назад
@George Michael Lately I have been using FlixZone. Just search on google for it =)
@markcory215
@markcory215 3 года назад
@Titan Achilles Definitely, I have been using Flixzone for years myself =)
@georgemichael9481
@georgemichael9481 3 года назад
@Titan Achilles Thank you, I signed up and it seems like a nice service :D I appreciate it!
@titanachilles7627
@titanachilles7627 3 года назад
@George Michael no problem xD
@nageshthotakura8758
@nageshthotakura8758 3 года назад
You are amazing. Thank you for your efforts.
@codewithbubb
@codewithbubb 3 года назад
Thanks very much, appreciate your support!
@user-js3zy8pk9o
@user-js3zy8pk9o 3 года назад
Thank you for this video!
@codewithbubb
@codewithbubb 3 года назад
You're welcome - hope you found it useful!
@Girlgirls12233
@Girlgirls12233 2 года назад
i don't understand why - i? and not just[ inStr.length - 1 ]
@marykhachatryan2049
@marykhachatryan2049 4 года назад
This was actually very useful! Thank you :)
@codewithbubb
@codewithbubb 4 года назад
Thanks Mary! Glad you found it useful😀
@viewsbyblur
@viewsbyblur Год назад
Could someone explain the inStr[inStr.length - 1 - i] I’m confused on this.
@Stancehall
@Stancehall Год назад
You still need help?
@viewsbyblur
@viewsbyblur Год назад
@@Stancehall if you could explain the statement above yes that’s be very helpful, thanks!
@Corumt
@Corumt 3 года назад
Can you do something on how to get odd and even numbers using arrays
@codewithbubb
@codewithbubb 3 года назад
Sure, i'll put something together.
@kremowydzien
@kremowydzien 5 лет назад
I didn't think by myself about turn to lower case and removing the non-alphanumerical characters. I did it after watching the video. I used this solution: const isPalyndrome = (str) => { str = str.replace(/[^a-z]/gi, "").toLowerCase(); const reverse = str.split("").reverse().join("").toLowerCase(); return str === reverse; }
@codewithbubb
@codewithbubb 5 лет назад
Yep, good practice to lower case everything to make sure it matches. I like the Regex to remove the non-alpha characters (could also use \W to match non-alphanumeric too 😉).
@EduardoOviedoBlanco
@EduardoOviedoBlanco 3 года назад
I think your first solution was better than the second one
@uaplatformacomua
@uaplatformacomua 5 лет назад
So hard I got the issue
@selchukkarakus6169
@selchukkarakus6169 4 года назад
This code doesn't work with strings containing - (hyphen)or _ (underscore) eg "_eye" or "0_0 (: /-\ :) 0-0" ?
@codewithbubb
@codewithbubb 4 года назад
Yeah, the \W word boundary won't pick these up and remove them (I would consider hypens to be part of words but not sure about underscores?). You can adjust your regex to suit your requirements / rules of what should be considered part of the palindrome. e.g /[^a-z]/gi will remove anything that's not actually a letter. Hope that helps?
@velmurugan.personel
@velmurugan.personel 5 лет назад
Why not use this i++ instead of i+=1 and inStr[inStr.length - 1] instead of this inStr[inStr.length - 1 -i ]
@codewithbubb
@codewithbubb 5 лет назад
Yes, i++ is fine however it's discouraged in a lot of JavaScript style guides (like AirBnB) so ES Lint complains about it. I've just got in to the habit of using that format now!
@velmurugan.personel
@velmurugan.personel 5 лет назад
@@codewithbubb Thanks your response man keep rocking.......
@codewithbubb
@codewithbubb 5 лет назад
You're welcome! Thanks for your comment also, it's good to question things all the time!
Далее
5 JavaScript Concepts You HAVE TO KNOW
9:38
Просмотров 1,4 млн
How to reverse a String in JavaScript Tutorial
5:06
Просмотров 32 тыс.
Palindrome Checker App | HTML, CSS & Javascript
18:30
Просмотров 2,9 тыс.
Do You Know Enough JavaScript To Learn React
6:28
Просмотров 422 тыс.
JavaScript Loops Made Easy
10:52
Просмотров 169 тыс.
Top 6 React Hook Mistakes Beginners Make
21:18
Просмотров 572 тыс.
Saving User Input in JS Objects
8:00
Просмотров 200 тыс.
How To Solve Algorithms - Longest Common Prefix
9:31