Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialali mur
UX Design Techdegree Student 23,377 PointsSearch the characters of a string in the array and return the index values as a new array
I am trying to write a script to search letters of a string inside an alphabet array, and return a new array with the index values of the alphabet array.
//created a string of whose characters I will be searching.
var text="Hello World"
//set it equal to lower case, so that I dont have to deal with the uppercase letters
var lowertext=text.toLowerCase();
//used split method to store the string characters as an array
var lowertextarray=lowertext.split("");
//created an alphabet array in which i will search the characters of the text variable
var alphabet=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
//created an empty array for store the newly created index values as array
var indices=[];
//started a loop which iterates in alphabet and lowertext array, and then I try to check if they are equal, and if their values are equal I try to store them inside indices array.
//sorry for the annoying comments :)
for (var i=0; i<alphabet.length; i++){
if (alphabet[i]===lowertextarray[i]) indices.push(i);
}
console.log(indices);```
4 Answers
minh nguyen
55,848 Points// hope this solve your issue, for every element in the alphabet, I loop through the "text" array and compare. if those //match if push the index to the indices array.
var text="Hello World"
var lowertext=text.toLowerCase();
var lowertextarray=lowertext.split("");
var alphabet =['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
var indices=[];
for (var i=0; i<alphabet.length; i++){
for (var j=0; j<lowertext.length; j++){
if (alphabet[i]===lowertextarray[j]) {
indices.push(i);
}
}
}
console.log(indices);
Steven Parker
231,898 PointsYou didn't say what issue you are having.
But as is, this program will always log an empty array. Despite the comments, I don't think I understand what your intention is. But the code inside your loop is probably not aligned with those intentions (though it does exactly what the comments say):
if (alphabet[i]===lowertextarray[i]) indices.push(i);
The alphabet array contains 26 elements, but the lowertextarray contains only 11. So for most of the loop, one side of the comparison will be undefined. And for the rest, for this particular string there are no cases where the letters happen to be equal.
Perhaps you need an inner loop to iterate the lowertextarray separately? But I'm still not sure what the overall objective is.
Steven Parker
231,898 PointsNow that I see what you were after, you could eliminate the alphabet and the outer loop entirely and do this:
var text = "Hello World"
var lowertext = text.toLowerCase();
var indices = [];
for (var i = 0; i < lowertext.length; i++) {
let val = lowertext.charCodeAt(i) - 97;
if (val >= 0 && val < 26)
indices.push(val);
}
console.log(indices.sort((a, b) => a - b));
ali mur
UX Design Techdegree Student 23,377 PointsYep!
Jason Anello
Courses Plus Student 94,610 PointsI think you'll be missing out on the letter 'a'. Your if
condition only covers 25 values.
This solution is a lot more efficient. You're only going through the text 1 time. With the nested for loops, you're going through the text 26 times.
Steven Parker
231,898 PointsGood catch on the lower limit, I corrected the code.
Jason Anello
Courses Plus Student 94,610 PointsThe code for 'a' is 97. So if you have an 'a' in the text then wouldn't val
equal 0? And then your if condition won't be true. If I understand the intent of the code, it should be pushing a zero.
ali mur
UX Design Techdegree Student 23,377 PointsOne more thing, the results are getting aligned from smallest to the largest, where does it happen exactly? Does it happen when i ask alphabet[i]===lowertextarray[j] ? For example, the result should be 15, 2, 20, but i get 2, 15, 20.
Steven Parker
231,898 PointsI had to sort it to get the same order.
So just skip the sort if you want it in original text order:
var text = "Hello World"
var lowertext = text.toLowerCase();
var indices = [];
for (var i = 0; i < lowertext.length; i++) {
let val = lowertext.charCodeAt(i) - 97;
if (val >= 0 && val < 26)
indices.push(val);
}
console.log(indices);
The original came out sorted because each letter of the alphabet is searched in order in the outer loop.
ali mur
UX Design Techdegree Student 23,377 Pointscan you please explain the logic in this code line by line? I just dont understand what you are doing.
let val = lowertext.charCodeAt(i) - 97;
if (val >= 0 && val < 26)
indices.push(val);
}
console.log(indices.sort((a, b) => a - b));
Steven Parker
231,898 PointsThis is a more efficient way of getting what you wanted.
let val = lowertext.charCodeAt(i) - 97;
Instead of looping through the alphabet and comparing, I take the character code of each letter and subtract 97, which is the code for 'a'. This produces the index number you wanted with 0 for 'a', 1 for 'b', etc.
if (val >= 0 && val < 26) indices.push(val);
Here, I only push the value only if it is in the range for a letter, so things like spaces (which the original program would not have found) are skipped.
console.log(indices.sort((a, b) => a - b));
Finally I sort the output to match what the original program did, which as I said before you can leave out if you prefer the numbers in the same order as the letters in the string. Since the sort function normally works on strings, I provide a custom comparison function as the argument so that it will compare numbers instead.
Jason Anello
Courses Plus Student 94,610 PointsJason Anello
Courses Plus Student 94,610 Pointsfixed code formatting