Reputation:
What I'm trying to do:
I'm trying to calculate the distance between two words in a string (in our example, var someString = "one two three four five six seven"
. As you can see in my code below, I convert the string to an array with the split()
method, then call the function minDist()
that calculates the distance between the words.
What's the problem:
As you can see in the code and my relevant comments, everything works as intended if I call the function using the string (or rather, the array, "one", "two", "three", "four", "five", "six", "seven"
) directly. The result is, as expected, 3 - which is the distance between "one" and "four" in this specific example.
However, as you can see, if I try to call the function using the array's variable, the result is null.
Why is that and how can I resolve this problem?
var someString = "one two three four five six seven"
var distanceCheck = someString.split(" ");
console.log("distanceCheck",distanceCheck,"Quick test. As expected, the result is: one,two,three,four,five,six,seven")
for (var i = 0; i < distanceCheck.length; i++) {
distanceCheck[i] = '"' + distanceCheck[i] + '"';
}
var list2b = distanceCheck.join(", ");
console.log("list2b",list2b,'As expected, the result is: "one", "two", "three", "four", "five", "six", "seven"')
var check0 = minDist(["one", "two", "three", "four", "five", "six", "seven"], "one", "four");
console.log("check0",check0,"Result is: 3")
var check1 = minDist([list2b], "one", "four");
console.log("check1",check1,"Result is: null. WHY?")
function minDist(words, wordA, wordB) {
var wordAIndex = null;
var wordBIndex = null;
var minDinstance = null;
for (var i = 0, length = words.length; i < length; i++) {
if (words[i] === wordA) {
wordAIndex = i;
}
if (words[i] === wordB) {
wordBIndex = i;
}
if (wordAIndex !== null && wordBIndex !== null) {
var distance = Math.abs(wordAIndex - wordBIndex);
if (minDinstance === null || minDinstance > distance) {
minDinstance = distance;
}
}
}
return minDinstance;
}
Upvotes: 0
Views: 134
Reputation: 493
Use this corrected snippet
In one you were sending an array of value in other you were sending a single value of array. 2nd mistake was appending "" after and before in the values; string values are already quoted in the array in js you need not add quotes.
I have corrected both of them and you can see both are returning 3.
I have corrected these lines
var check1 = minDist(distanceCheck, "one", "four");
and you can omit this loop
//for (var i = 0; i < distanceCheck.length; i++) {
//distanceCheck[i] = '"' + distanceCheck[i] + '"';
//}
var someString = "one two three four five six seven"
var distanceCheck = someString.split(" ");
console.log(distanceCheck, "Quick test. As expected, the result is: one,two,three,four,five,six,seven")
//for (var i = 0; i < distanceCheck.length; i++) {
//distanceCheck[i] = '"' + distanceCheck[i] + '"';
//}
var list2b = distanceCheck.join(", ");
console.log("list2b", list2b, 'As expected, the result is: "one", "two", "three", "four", "five", "six", "seven"')
var check0 = minDist(["one", "two", "three", "four", "five", "six", "seven"], "one", "four");
console.log("check0", check0, "Result is: 3")
var check1 = minDist(distanceCheck, "one", "four");
console.log("check1", check1, "Result is no longer null.")
function minDist(words, wordA, wordB) {
var wordAIndex = null;
var wordBIndex = null;
var minDinstance = null;
for (var i = 0, length = words.length; i < length; i++) {
if (words[i] === wordA) {
wordAIndex = i;
}
if (words[i] === wordB) {
wordBIndex = i;
}
if (wordAIndex !== null && wordBIndex !== null) {
var distance = Math.abs(wordAIndex - wordBIndex);
if (minDinstance === null || minDinstance > distance) {
minDinstance = distance;
}
}
}
return minDinstance;
}
Upvotes: 1
Reputation: 178011
Because [list2b] is not what you expect.
It is an array with one string:
"\"one\", \"two\", \"three\", \"four\", \"five\", \"six\", \"seven\""
As you can see it additionally has extra quotes
Upvotes: 1
Reputation: 175
First, you do not need to add "" to a string if you pretend to use it as an array. Your function expects an array of strings, not a single string. In the snippet i show you a working version. Because you are using a string for the array, the way to go is to get it back to a real array with split(",") (Note that i got rid of the .join(",")).
var someString = "one two three four five six seven"
var distanceCheck = someString.split(" ");
console.log("distanceCheck",distanceCheck,"Quick test. As expected, the result is: one,two,three,four,five,six,seven")
for (var i = 0; i < distanceCheck.length; i++) {
distanceCheck[i] = '' + distanceCheck[i] + '';
}
var list2b = distanceCheck.join();
console.log("list2b",list2b,'As expected, the result is: "one", "two", "three", "four", "five", "six", "seven"')
var check0 = minDist(["one", "two", "three", "four", "five", "six", "seven"], "one", "four");
console.log("check0",check0,"Result is: 3")
var check1 = minDist(list2b.split(","), "one", "four");
console.log("check1",check1,"Result is: null. WHY?")
function minDist(words, wordA, wordB) {
var wordAIndex = null;
var wordBIndex = null;
var minDinstance = null;
for (var i = 0, length = words.length; i < length; i++) {
if (words[i] === wordA) {
wordAIndex = i;
}
if (words[i] === wordB) {
wordBIndex = i;
}
if (wordAIndex !== null && wordBIndex !== null) {
var distance = Math.abs(wordAIndex - wordBIndex);
if (minDinstance === null || minDinstance > distance) {
minDinstance = distance;
}
}
}
return minDinstance;
}
Upvotes: 1
Reputation: 124
This is because list2b is one string, and [list2b] is an array with only one value. If you remove the for loop and replace [list2b] with distanceCheck it should work.
Upvotes: 0