Jump to content

Good old palindrome in javascript.

mrchow19910319
Go to solution Solved by jincio,

So, yeah, you can't compare Arrays. That's why you usually use library like Lodash for doing such thing. For the last case on other hand, you are initializing a new empty object

let new_string = {};

 so when you try to add a string to an object javascript try to convert everything to a string and for that use the method `.toString()` on the obejct, which result in [object Object]. For making the last example work you need to initialize the new object as a new empty string

let new_string = "";

 

I am trying to solve this how to detect a word is palindrome quest using javascript where I found some logic error in my code I do not know how to solve. 

 

Let's say that we ignore all the edge cases, like uppercase/lowercase of the word, non-letter characters etc.

And the user only key in a single word with lowercase in it.

 

There are a few ways you can do this. 

 

1. use javascript built in function. (this one works)

function check (string){
	let new_string = string.split('').reverse().join('');

	if (string === new_string){
		return true;
	}
	return false;
}


console.log(check("helloaxd"));
console.log(check("eye"));

I think that we have not much to say, all the work is done using split() and reverse() and join()

 

2. Split old string into array, using for loop to create a new reversed array, then use join to make it a reversed string (this one works)

function check(string){
	let arr = string.split('');
	let new_arr = [];

	for(let i = arr.length-1; i>=0; i--){
		new_arr.push(arr[i]);
	}

	let new_string = new_arr.join('');

	if(string===new_string){
		return true;
	}
	return false;
}


console.log(check("helloasd"));
console.log(check("hasd"));

since the above snippet is still comparing strings, it works. 

now it gets strange.

3. convert everything to array then compare them (DOES NOT WORK)

function palindrome(str) {
  let arr = str.split('');
  let reversed = [];

  for(let i = arr.length-1;i>=0;i--){
    reversed.push(arr[i]);
  }

  console.log("reversed is:" + reversed);
  console.log("original is:" + arr);

  if(reversed===arr){return true;}
  else{return false;}

}



console.log(palindrome("eye"));

this will give you false no matter what. I think that we cannot compare arrays like that can we? 

4. reverse string returned an object?? 

function check(string){
	let new_string = {};

	for(let i = string.length-1; i>=0; i--){
		new_string = new_string + string[i];
	}

	console.log("string is " + string);
	console.log("reversed string is: " + new_string);
	return new_string === string;
}


console.log(check("hello"));

in this snippet, new_string will give you 

[object Object]olleh

What does it mean and why is that so? 

 

If it is not broken, let's fix till it is. 

Link to comment
Share on other sites

Link to post
Share on other sites

So, yeah, you can't compare Arrays. That's why you usually use library like Lodash for doing such thing. For the last case on other hand, you are initializing a new empty object

let new_string = {};

 so when you try to add a string to an object javascript try to convert everything to a string and for that use the method `.toString()` on the obejct, which result in [object Object]. For making the last example work you need to initialize the new object as a new empty string

let new_string = "";

 

Link to comment
Share on other sites

Link to post
Share on other sites

Here is another way for you :)

it is a waste of processing time to create a new string to compare when you compare the input to itself.

const isPalindrome = (str) => {
  const length = str.length
  for(let i = 0; i < length; i++){
  	// has to have +1 else its out of the bound of the array
  	if(str[i] !== str[length-(i+1)]) return false
  }
  return true
}

 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, vorticalbox said:

Here is another way for you :)

it is a waste of processing time to create a new string to compare when you compare the input to itself.


const isPalindrome = (str) => {
  const length = str.length
  for(let i = 0; i < length; i++){
  	// has to have +1 else its out of the bound of the array
  	if(str[i] !== str[length-(i+1)]) return false
  }
  return true
}

 

after finding 7 ways of checking palindrome

Spoiler

787864503_DragonBallFighterZHowToSummonShenron.jpg.192c8c913ddfe1eebfc8c1aa49ad5d32.jpg

just kidding. thanks for the input! 

I think that doing all these algorithms is gonna prepare me for technical interviews.

If it is not broken, let's fix till it is. 

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, mrchow19910319 said:

after finding 7 ways of checking palindrome

  Reveal hidden contents

787864503_DragonBallFighterZHowToSummonShenron.jpg.192c8c913ddfe1eebfc8c1aa49ad5d32.jpg

just kidding. thanks for the input! 

I think that doing all these algorithms is gonna prepare me for technical interviews.

you could run some benchmarks and see which is the fastest :P I think mine will be as it returns false on the first fail without needing to build the whole string /array.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

35 minutes ago, vorticalbox said:

you could run some benchmarks and see which is the fastest :P I think mine will be as it returns false on the first fail without needing to build the whole string /array.

haha yeah! when I have time I will check that! 

 

what language you main? 

If it is not broken, let's fix till it is. 

Link to comment
Share on other sites

Link to post
Share on other sites

5 hours ago, vorticalbox said:

Here is another way for you :)

it is a waste of processing time to create a new string to compare when you compare the input to itself.


const isPalindrome = (str) => {
  const length = str.length
  for(let i = 0; i < length; i++){
  	// has to have +1 else its out of the bound of the array
  	if(str[i] !== str[length-(i+1)]) return false
  }
  return true
}

 

o(n) in all of it's beauty

Link to comment
Share on other sites

Link to post
Share on other sites

18 hours ago, vorticalbox said:

Here is another way for you :)

it is a waste of processing time to create a new string to compare when you compare the input to itself.


const isPalindrome = (str) => {
  const length = str.length
  for(let i = 0; i < length; i++){
  	// has to have +1 else its out of the bound of the array
  	if(str[i] !== str[length-(i+1)]) return false
  }
  return true
}

 

the loop condition only needs to be length/2, not the full length of the string

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×