
Stacks (Data Structure) - Beau teaches JavaScript
video description
Date: 2022-03-14
Related videos
Comments and reviews: 6
Eldar
function isPalindrome(str) -
for (let i=0; i < str.length - i; i++) -
if(str[i]!==str[str.length-1-i]) return false
-
return true
-
You can access string with [i]ndexes.
You don't need to use additional variables (except iterator).
You don't need to modify the original string or split it into an array and modify it (with pop and especially shift).
You may add a length>0 check if you feel like an empty string is not a palindrome.
Comments are welcome.
UPD. I forgot to mention that it will work with arrays too.
reply
function isPalindrome(str) -
for (let i=0; i < str.length - i; i++) -
if(str[i]!==str[str.length-1-i]) return false
-
return true
-
You can access string with [i]ndexes.
You don't need to use additional variables (except iterator).
You don't need to modify the original string or split it into an array and modify it (with pop and especially shift).
You may add a length>0 check if you feel like an empty string is not a palindrome.
Comments are welcome.
UPD. I forgot to mention that it will work with arrays too.
reply
blehbleh
Great video! One question,
Even though pop() removes the top element from the stack, the storage array still preservers the same size (for example, say we've pushed onto storage [10,20,40], then popped the stack, we get [10,20,], yet when doing console.log(stack.storage.length), we still get 3, not 2.
reply
Great video! One question,
Even though pop() removes the top element from the stack, the storage array still preservers the same size (for example, say we've pushed onto storage [10,20,40], then popped the stack, we get [10,20,], yet when doing console.log(stack.storage.length), we still get 3, not 2.
reply
Siderite
Beau, I love your videos, but please be more careful. There are a bunch of mistakes in almost every video you make. You can easily edit them or even redo them. A stack is not a First In First Out, that's a queue. I don't mind small mistakes, but if they are perennial they become annoying.
reply
Beau, I love your videos, but please be more careful. There are a bunch of mistakes in almost every video you make. You can easily edit them or even redo them. A stack is not a First In First Out, that's a queue. I don't mind small mistakes, but if they are perennial they become annoying.
reply
kashinath
One Question; what if we peek on empty stack here ? Shouldn't we check in peek function whether count is 0 or not ?
Is it something like it should return -1 in case of empty stack ? But here storage object don't have -1 as a key... suggestions would be appreciated.
reply
One Question; what if we peek on empty stack here ? Shouldn't we check in peek function whether count is 0 or not ?
Is it something like it should return -1 in case of empty stack ? But here storage object don't have -1 as a key... suggestions would be appreciated.
reply
Jonathan
function isPalindrone(str) -
var stack = str.split('')
var isPalindrone = true
while(isPalindrone === true && stack.length > 1) -
isPalindrone = stack.shift() === stack.pop()
-
return isPalindrone
-
reply
function isPalindrone(str) -
var stack = str.split('')
var isPalindrone = true
while(isPalindrone === true && stack.length > 1) -
isPalindrone = stack.shift() === stack.pop()
-
return isPalindrone
-
reply
Bereket
Excellent tutorial. but, You mentioned that, Stack is FIFO-First in First Out data structure. Stack is Not FIFO. Queue is FIFO. Stack is FILO or LIFO.
reply
Excellent tutorial. but, You mentioned that, Stack is FIFO-First in First Out data structure. Stack is Not FIFO. Queue is FIFO. Stack is FILO or LIFO.
reply
Add a review, comment
Other channel videos















