我的目標是比較文本文件的第一行輸入從表單中接收的輸入。 我的代碼成功地從文件中讀取,之後我用下面的代碼比較文件中的行與從javascript中的表單輸入
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
我的問題是,這兩個句子都是平等的,但在控制檯說,假以
console.log(sentence==input);
這怎麼可能,和爲什麼。
try console.log(sentence.trim()== input.trim()); – shreesha
句子和輸入到底是什麼? console.log的作用是什麼? – Liam
你也應該使用'==='而不是'==',[見這裏](http:// stackoverflow。com/questions/359494/does-it-matter-which-equals-operator-vs-i-use-in-javascript-comparisons) – Liam