Maxlength conditional statement

How can I get the maxlength of the character in my services.js and put the maxlength of the character in my textbox ?

Here is my code:

   services.js
            .factory('FactoryofQuestions', function() {
        
        {
            
                question: "RO RORPOEAT",
                answer: "or operator"
               
            },
            {
           
                question: "UARNGLA",
                answer: "angular"
              
            },
            {
            
                question: "AAJVRTSCIP",
                answer: "javascript"
                
            },
    
     ];
    
    

    {{question}}

    <input type="text" name="answer">

image

Idea 1
In addition to the properties question, answer, you could add one called maxCharLength and it could look something like :

maxCharLength : function(){
    return this.question.length;
}

Then you can do something like, {{FactoryQuestion[0].maxCharLength()}} in your view.

Idea 2
You could just do something like {{FactoryQuestion[0].question.length}} though I think the first way is better and also gives you an idea of creating functions for objects, which will come in handy later on.

I’m sorry ,but I didn’t understand it clearly, can you please give me an full example of code of what your saying, so I can understand ? Thanks :smile:

Sure, I’ll do that right now. But just a heads up, I had a question similar on a few interviews, so make sure you understand. Ask me to clarify if you don’t.

1 Like

http://jsfiddle.net/66ea6y3z/

Here is my first solution.

Each object you are creating in the factory, which is a question in your case has 3 properties: Question, Answer and maxCharLength.

Question and Answer are both strings, while maxCharLength is a function, which returns the length of the question.

So question[0].question will return “RO RORPOEAT” while maxCharLength will return 11.

In my example, the questions array is basically the array you made in your factory.

So you can call the maxCharLength function by doing something like questions[0].maxCharLength();

Does that help and make sense?

EDIT

Heres my second solution. Less code, maybe easier at first, but I think the first way demonstrates and gives you an idea of some cool functions you can do in Javascript.

http://jsfiddle.net/0b72q2ob/

Yes, it makes sense to me now , but Does these two solutions has the same output ?

Yep they should have the exact same output since they are both returning The length of the question.

There shouldn’t be a difference between the two. The first is just more Javascript/technical and similar to what an interviewer might ask you.

how about transfering the character length we get from the array into the textbox ?and can you please include also the output of the code that you’ve make ? so that I can see how it works ? by the way thanks for the help :smile:

Haha before I do that, give me your attempt or how you think it should be done. It’s definitely possible, but I don’t want to give you all the answers.

I’m asking this question to know how can I solve this problem , i need your help man , I’m newbie here in ionic , I’m also building my own app. and I want to learn more about here in ionic so please help me. :pensive:

Haha no worries, just making sure you weren’t having me do the work :wink:

So what you need is an input element, with the value initialized to the length of the question? Is that correct? Just want to make sure I’m helping you out right.

Yup , Thanks man :joy:

Is this what you need?

I’m not too sure on what exactly you need, but this will put the character length in the input box. Just change the index in the controller to change which question you are referencing. Hope this helps.

Thanks for the example man, you surely put the max character length into the textbox , but here’s I want to happen in the textbox , i want the textbox to accept the max character length that we get from the array . uhm. not to display it , hehe :grinning:

Sorry, I don’t really understand what you need. I think you should try writing it yourself, see where you can get to, and I’ll help debug or give you hints. I gave you more than enough to work with.

BTW, quick tip. When I approach any problem in programming, the first thing I do is try and break it down into smaller chunks and puzzle pieces. At first it can seem crazy and daunting, but if you break it down, its really just putting the pieces together.

Like ok I need an input for the user -> I can use HTML input element.
I need questions -> I can create a factory for each question
I need length of each question -> I can write a function to return the question length

See what I mean? I think you got this, just gotta start coding and give it a try :slight_smile:

2 Likes

Hi @DaDanny , Can you help me , I can’t get the questions from the factory , here is my factory.js

.factory('MyQuestions', function(){
  var questions = [
    {
            question: "RO RORPOEAT",
            answer: "or operator",
            maxCharLength: function(){
               return this.question.length;
            }
        },
        {
            question: "UARNGLA",
            answer: "angular",
            maxCharLength: function(){
                return this.question.length;
        	}
         },
    	 {
           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;
            }


           } 

 };

The first issue I see is in your return statement. It looks like you are calling “shuffleArray” which I assume is a function you wrote? I’m not seeing it in the factory, so you’ll probably get a “function shuffleArray is undefined” error if it is not defined within that factory.

Next, you can just return the questions like in this codepen:

And then in your controller, you can do whatever you want with the questions.

If you want to create a getQuestion method for the factory, which is a good idea since it’ll help you getting used to writing factory methods, I would do something like this

//Function for getting specific question
var getQuestion = function(index){
   if(index < questions.length){
       return questions[index];
   } else return false;

 /*
  At the bottom of your factory, you can return or expose 
  any methods you wrote
 */
return {
  getQuestion : getQuestion

}

Hope that helps :slight_smile:

1 Like

I’m sorry I forgot to include the shuffle(array) function :sweat_smile: , but is okay , that’s not my problem.
My probem is How can i call my var questions array from my factory and include it into my controller , I’m having a problem with this part of my factory.js

return {
getQuestion: function(id) {
 
    if(id < questions.length) { //I don't know where can I call my "var questions"
        return questions[id]; // and include it to my controller
        
    } else {
        return false;
    }


   } 

Unlike you did in you example its, very simple , I’m confused with my code.

//this is your factory.js

.factory
        return{
            questions: questions
          };

//and this your .controller

.controller
    $scope.questionNum = 0; 
  
  $scope.questions = MyQuestions.questions;

If you don’t return anything from your factory, you won’t have access to anything defined in it.

So in that example, when I do

return {
   questions : questions
}

What I’m basically doing is allowing you to access the questions variable of the factory in the controller. Which you can do by

MyQuestions.questions

So now in the controller, we can access the questions.

You are doing this as well with the getQuestion function. Now you can call MyQuestion.getQuestion(id) and it should run that function you defined within your factory.

If you want to be able to access both the questions and your getQuestion method, then you can return both by just doing:

return {
     getQuestion: function(id) {
         // your code
     },
     questions : questions
}

Though there isn’t really a point in doing both. But that is how you could return multiple things from your factory.

Hi , I tried to put this into my code:

questions : questions

into this code you gave to me:

return {

        getQuestion: function(id) {
           shuffleArray(questions);
            if(id < questions.length) {
                return questions[id];
                
            } else {
                return false;
            }
        },
        questions : questions
}

And I put this to my controller :I followed the code example you have given to me:

$scope.questions  =  wordFactory.questions;  // The name of my Factory is wordFactory

And This is my input box where I the input box will determine if how many maxlength of the characters of an array:

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

I’ve tried to follow the examples you gave me but , nothing happened in the output :frowning: Please help me . :cry: