0

My aim is to compare the first line of a text file to input recieved from a form. My code successfully reads from the file,after that I use the following code

  var lines=result.split("\n");   //this line turns file into array
  var line=lines[i].split(" ");   //each index is further changed into an   array         
  var sentence=lines[i].split(" ").join(" ").toLowerCase();   //this turns the line/sentence from an array into a String
  var input=document.getElementById('inputFromForm').value.toLowerCase(); //this line get input from form

 console.log(sentence);    //this prints the first line of the text file to the console
 console.log(input);     //this prints the inputFromForm to the text file
 console.log(sentence==input);  //check for equality

My problem is that both sentences are equal but the console says false to

 console.log(sentence==input);

How is that possible,and why.

Liam
  • 27,717
  • 28
  • 128
  • 190

3 Answers3

2

You can use different approaches.

Approach I:

    var str1 = "ab abcde";
    var str2 = "ab abcde";
    var n = str1.localeCompare(str2);
    document.getElementById("demo").innerHTML = n;
  • Returns -1 if str1 is sorted before str2
  • Returns 0 if the two strings are equal
  • Returns 1 if str1 is sorted after str2

For more details check: http://www.w3schools.com/jsref/jsref_localecompare.asp

Approach II:

You can match each word in the sentence using a for loop.

var s1 = 'Example 1';
var s2 = 'Example 2';

var s1Array= s1.split(' ');
var s2Array= s2.split(' ');

var result = 0;

for(var i = 0; i<s1Array.length; i++)
{
     if(s1Array[i] !== s2Array[i]){
         result=-1;   
         return;
     }
}

if(result<0){//strings not equal}

This approach can also be used to find how much similar the two sentences are. I have mentioned this approach here as you are splitting the sentence into array, so you can use this approach too.

Ankit
  • 1,075
  • 1
  • 8
  • 19
1

Check like this

Syntax :

str1.localeCompare(str2)

Explanation of localeCompare: It will

  • Returns -1 if str1 is sorted before str2
  • Returns 0 if the two strings are equal
  • Returns 1 if str1 is sorted after str2

So

Console.log(sentence.localeCompare(input));

So your two strings are equal it will provide 0.

vinodh
  • 714
  • 1
  • 7
  • 20
0

try the below methods

console.log(sentence.trim()==input.trim());

console.log(sentence.toLowerCase()==input.toLowerCase());

console.log(sentence.localeCompare(input)==0);
shreesha
  • 1,811
  • 2
  • 21
  • 30
  • [use ===](http://stackoverflow.com/questions/359494/does-it-matter-which-equals-operator-vs-i-use-in-javascript-comparisons) – Liam Sep 07 '15 at 15:10
  • both are string what is the use of === – shreesha Sep 07 '15 at 15:11
  • [*My advice is to never use **the evil twins**. Instead, always use === and !==. All of the comparisons just shown produce false with the === operator*](http://stackoverflow.com/questions/359494/does-it-matter-which-equals-operator-vs-i-use-in-javascript-comparisons). `==` is very inconsistent in it's behaviour and should typically **never** be used. e.g. `"abc" == new String("abc") //false`, both are strings... – Liam Sep 07 '15 at 15:14