In your controller? Lets make sure you are getting the questions first.
Since you are having trouble, I would ignore my suggestion of the maxCharLength method. I thought it would be good to show you how you can write methods for objects, but I think it just made it more confusing.
I finally got it shows the length of the question but thereās only one more problem ,
Do you have an idea how can I get this Qid from the wordFactory
.factory('wordFactory', function(){
var questions = [
{
Qid:'1', // I want to call Qid into my controller
question: "RO RORPOEAT",
answer: "or operator",
maxCharLength: function(){
return this.question.length;
}
},
{
Qid:'2', // I want to call Qid into my controller
question: "UARNGLA",
answer: "angular",
maxCharLength: function(){
return this.question.length;
}
},
{
Qid: '3', // I want to call Qid into my controller
question: "AAJVRTSCIP",
answer: "javascript",
maxCharLength: function(){
return this.question.length;
}
}
]
return {
getQuestion: function(id) {
shuffleArray(questions);
if(id < questions.length) {
return questions[id];
} else {
return false;
}
}
};
and call it into my controller and insert it here questions[insertQidHere].question.length ?
Iām not sure I see the point of QID. In an array, each item has its own index and it looks just like QID in your example. What are you using QID for that index would not work for?
You are trying to use QID like an index, when it is a property. I suggest just using the index of the item in the array.
As far as getting the index of any array, you can either use Javascripts array.indexOf method, or just write a for loop, iterate through your array searching for the index you need and then save the index once you find it.
Ok, so you asked for a way to find the index in an array without using any specific number. Since your array, questions, has objects, you canāt use indexOf to find the index of the question you are looking for.
So what youāll need to do is loop through the array, and search for the question you want. You compare the question property at each index in the questions array to see if you have found the one you are searching for. Once you find it, you know what the index (location) of that object is save that index and break out of the loop.
Thats the first example in the jsFiddle.
The second part of the example is how you could use indexOf. If your array is made up of strings, numbers or booleans, (basically anything that isnāt an object), you can us the array.indexOf method to find it without writing a for loop. And thats what I did in the second part.
I hope this clears things up and helps you out. I think we have deviated away from your original question quite a bit, but Iām happy to help get back on track
Thanks for the effort for helping me
Now that we got the indexes of the arrays , can we call now the questions of the array and put the maxcharacter length of it into the input box ?
Itās okay,Actually Iām making a quiz application as you can see on my post above thereās a next question and submit button , when you click this next question button you will proceed to the next question and the submit button is also just like the next question function but the submit button will determine whether if the answer is wrong or correct .
Now my problem is all my questions are all shuffled , I need to get the max character length of every questions in my array and make the textbox get the max character length of every question it match.
The example you gave to me is correct but it only finds the specific index of an array in ordered .Thatās why Iāve put the Qid to determine the shuffled questions array but , I donāt know How can I call this Qid in my controller and insert it into the input textbox.