I'm just playing around with Javascipt as an exercise. I just want to push the index number of every space in my string, into an array. No luck though, my array is coming up empty - or full of spaces, I'm not sure which. Any help would be gracefully received. :)
function checkText() { var str1 = document.getElementById("texty").innerHTML; var dexArray = []; for (var i = 0; i < str1.length; i++) { if (str1.indexOf(str1[i]) === " ") { dexArray.push(str1.indexOf(str1[i])); } } alert(dexArray); }
<p id="texty">Mary had a little lamb</p> <button onclick="checkText()">Click</button>
You need only the check against the character at position i
and push it to the result if found.
function checkText() { var str1 = document.getElementById("texty").innerHTML; var dexArray = []; for (var i = 0; i < str1.length; i++) { if (str1[i] === " ") { dexArray.push(i); } } console.log(dexArray); }
<p id="texty">Mary had a little lamb</p> <button onclick="checkText()">Click</button>
A version with indexOf
.
function checkText() { var str1 = document.getElementById("texty").innerHTML, dexArray = [], i = str1.indexOf(' '); while (i !== -1) { dexArray.push(i); i = str1.indexOf(' ', i + 1); // change start postion for search } console.log(dexArray); }
<p id="texty">Mary had a little lamb</p> <button onclick="checkText()">Click</button>
It is because of your if-condition
, indexOf
returns a number
instead of a string
let alone a empty space character ' '
.
function checkText() { var str1 = document.getElementById("texty").innerHTML; var dexArray = []; for (var i = 0; i < str1.length; i++) { str1.charAt(i) == ' ' && dexArray.push(i); } alert(dexArray); }
<p id="texty">Mary had a little lamb</p> <button onclick="checkText()">Click</button>
You should use comparison operator ==
or ===
instead indexOf
function checkText() { var str1 = document.getElementById("texty").innerHTML; var dexArray = []; Array.from(str1).forEach((x,i)=>{x === " " && dexArray.push(i) }); console.log(dexArray); }
<p id="texty">Mary had a little lamb</p> <button onclick="checkText()">Click</button>
function checkText() { var str1 = document.getElementById("texty").innerHTML; var dexArray = []; str1.split("").forEach(function(x,i){ x === " " && dexArray.push(i) }); console.log(dexArray); }
<p id="texty">Mary had a little lamb</p> <button onclick="checkText()">Click</button>
String.prototype.indexOf
returns index of the given substring and you are comparing it to the space ' '
. The whole use of indexOf
is pointless. If you really need to use indexOf
then you might want to look at the arguments this method takes and notoce that it can take an offset as the second parameter, but I always prefer the easier solution if I have one.
You can do something like this:
var str1 = 'whatever string you want to play with';
var arraySpaces = [];
var length = str1.length;
var i = -1;
while (++i < length) {
if (str1[i] === ' ') {
arraySpaces.push(i);
}
}
console.log(arraySpaces);
This way you have the actual indexes of spaces in your array.
Since this is an exercise you might want to check out Regexp.prototype.test and Regexp's lastIndex which allows for more convenient index searches if you have some longer substring to look for.
Good luck.
空了,你可以用 alert("1"+dexArray+"1") 测试,因为 str1.indexOf(str1[i]) 返回数字。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.