Maxlength conditional statement

What is the output when you do:

console.log('questions: ', $scope.questions)

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.

What happens when you try:

<input type='text' ng-value={{questions[1].question.length}}>

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 ?

If you want to get the qid, just do

var qid = questions[0].qid

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?

I tried to insert Qid in my array and get the Qid into my controller
and I put the Qid inside this,

ng-value="{{questions[Qi ].answer.length}}

Iā€™m having a problem of my array ,
How can I get the index of any array without using this or any specific number. :

.controller
questionsNum = 0;

<input type="text" name="answer" ng-value="{{questions[questionsNum ].answer.length}}"> // This is your example .

This is my code

  <input type="text" name="answer" ng-value="{{questions[Qid].answer.length}}"> / /I try to  insert the Qid here but nothing happened

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.

Uhm, can you please give me some example code of this array.indexOf about my problem so I can understand it . THank you for helping me :smile:

http://jsfiddle.net/n51132hL/1/

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 :smile:

1 Like

Thanks for the effort for helping me :smile:
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 ?

Yep, you already did that a few posts up:

<input type='text' ng-value={{questions[1].maxCharLength()}}>

Just replace 1 with the index of the question object you want.

but it only get the 1 specified index, uhm, Do you have any other ideas in getting indexes in getting ID of the array ?

Iā€™m a little lost on what exactly you need. Can you please explain what exactly you need done. I think you are over thinking this problem.

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.