0

DISCLAIMER: i'm legit a newbie

I have a 2nd parameter in the getInput function, I should use it for the 9 zeros that I should input. But I don't know how to loop it to become 9 zeros instead of putting it in a variable.

How do I loop and store 9 zero's into my "digit" parameter without declaring it as var zr = "000000000"

here's my code:


<html> 
<head> 
<title>Search</title> 
  <script>
    //This method does the processing

     function getInput(input, digit){
      var str=input.substring(0,input.length);
      var padd0=9-str.length;


      var zr="000000000";
      var zrsub=zr.substring(0,padd0);
      var output="A"+zrsub+""+str;

      //can also be var output=input[0]+zrsub+""+str;

     return output;
    }


    //Displays output
    function showOutput(){
       var input=document.getElementById("search-input").value;
       var dislay=document.getElementById("search-output");
       dislay.value=getInput(input);
    }

  </script>

</head> 

<body> 
  <div class="wrapper"> 

    <input type="text" id="search-input">
    <input type="button" id="btn" value="ENTER" onclick="showOutput()"> <br><br>
    <input type="text" id="search-output"> 

  </div>
</body> 

</html>

Sorry just a newbie in this whole programming thing. Just a little confused.

randombee
  • 13
  • 5
  • What is the expected output you want ? – virender nehra Oct 07 '19 at 02:53
  • when i input 1234, it will output A000001234. fixed to 9 numbers only or if I input 202 it will ouput A000000202 – randombee Oct 07 '19 at 02:58
  • @randombee check out https://stackoverflow.com/questions/2686855/is-there-a-javascript-function-that-can-pad-a-string-to-get-to-a-determined-leng – Nick Parsons Oct 07 '19 at 03:04
  • What's the problem with your approach? If the max length is 9 then it's ok to have zr of length 9 and take substring as per the input length. You can declare zr outside the function and use it as a const – gp. Oct 07 '19 at 04:23
  • @gp. uh yeah my instructor wants me to add a parameter that serves as a storage of zeros – randombee Oct 07 '19 at 04:27

2 Answers2

0

with for loop join string

function joinString(input,digit) {
  
  var inputArr = input.split("");
  // var n = 9; // the length of the ouput string;
  for (var i = 0; i < digit; i++) {
    inputArr.unshift(0);
    if (inputArr.length === digit) {
      return inputArr.join("");
    }
  }
}
console.log(joinString("123456"));
Community
  • 1
  • 1
virender nehra
  • 470
  • 6
  • 12
  • yes i tried that but I don't want to put 9 zeros because there may be 8 numbers or 6 that's why i chose to loop it. but i dont know how – randombee Oct 07 '19 at 03:48
  • that's not a problem you can pass in the input 1to9 any number based on the length of the input it add zero like you have a input "5331234" so the output is "005331234" – virender nehra Oct 07 '19 at 04:45
  • this worked thanks!! but how do I add "digit" as a new parameter so it would become the value of n = 9??? – randombee Oct 07 '19 at 05:21
  • oh nevermind i got it, it's supposed to be `console.log(joinString("123456",9));` am i right? thank you for your help! i appreciate it so much – randombee Oct 07 '19 at 05:53
0

You can use padStart

function getInput(input, digit){
    return 'A'+ input.toString().padStart(digit, '0');
}

document.getElementById('target').innerHTML = getInput(132,9)
<p id="target"></p>

IE may not support it though.

arif08
  • 746
  • 7
  • 15
  • yeah i tried it but didn't work. i just really need to store it in the parameter and loop it pls help me – randombee Oct 07 '19 at 03:57
  • Sorry.. didn't get the problem. it should work according the the documentations here.. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart Tell me what you are trying to achieve? Is the goal is to leverage that 2nd parameter? – arif08 Oct 07 '19 at 04:02
  • The digit parameter should contain the value of zeros and loop it like 0<9 or something because i dont want to store it in a simple variable. i just dont know how to loop it's my first time – randombee Oct 07 '19 at 04:09
  • I have edited.. try this. Assuming both the parameters will be numbers. And the digit parameter defines the total length of the string without the 'A' in the beginning . – arif08 Oct 07 '19 at 04:15
  • tried to input 123 for example. but the output is only A123 so it didn't work. – randombee Oct 07 '19 at 04:25
  • I have added a snippet. run it – arif08 Oct 07 '19 at 04:54
  • is it like this? please check https://codepen.io/randombee/pen/KKKKYjg – randombee Oct 07 '19 at 05:05
  • Where's your input for the 2nd parameter? and you have sent only one parameter to that function. – arif08 Oct 07 '19 at 06:16