-1

CODE WORKING

Numbers are randomizing this is fine.I need to change it like 0-9 numbers should not repeat when i click the buttons under "numbers" class which id's are "s1,s2,s3,s4...".

when i click it happens like

1 2 3               3   5   7                        3 5 2
4 5 6   not like   !4  !4   2 It must be like this   1 4 7
7 8 9     ---->     8  !0   9      ----->            8 0 9
0                  !0                                6

++++

The current value of button has to be set in textbox which id is "getir" when i click it.

This must be done with and I couldn't make it Javascript

function Random() {
    return (Math.floor(Math.random() * 10));
}

function randomValue() {
    var x = document.getElementsByClassName("numbers")
    var i;
    for (i = 0; i < x.length; i++) {
        x[i].value = Random()
    }
}

function silYazi() {
    document.getElementById("getir").value = "";
}
 <table>
            <tr>
                <input type="text" id="getir" value="" name="getir">
            </tr>
            <br>
            <tr>
                <input id="s1" type="button" class="numbers btn btn-outline-success mt-3" onclick="randomValue()"
                    value="1">
                <input id="s2" type="button" class="numbers btn btn-outline-success mt-3 ml-4" onclick="randomValue();"
                    value="2">
                <input id="s3" type="button" class="numbers btn btn-outline-success mt-3 ml-4" onclick="randomValue();"
                    value="3">
            </tr>
            <br>
            <tr>
                <input id="s4" type="button" class="numbers btn btn-outline-success mt-3" onclick="randomValue();"
                    value="4">
                <input id="s5" type="button" class="numbers btn btn-outline-success mt-3 ml-4" onclick="randomValue();"
                    value="5">
                <input id="s6" type="button" class="numbers btn btn-outline-success mt-3 ml-4" onclick="randomValue();"
                    value="6">
            </tr>
            <br>
            <tr>
                <input id="s7" type="button" class="numbers btn btn-outline-success mt-3" onclick="randomValue();"
                    value="7">
                <input id="s9" type="button" class="numbers btn btn-outline-success mt-3 ml-4" onclick="randomValue();"
                    value="8">
                <input id="s8" type="button" class="numbers btn btn-outline-success mt-3 ml-4" onclick="randomValue();"
                    value="9">
            </tr>
            <br>
            <tr>
                <input id="s0" type="button" class="numbers btn btn-outline-success mt-3" onclick="randomValue();"
                    value="0">
                <input id="btnSil" type="button" class="btn btn-outline-danger mt-3 ml-5" onclick="silYazi();"
                    value="Sil">
            </tr>
        </table>
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
  • So you want to set the value of `getir` to the value of `btnSil`? – Heretic Monkey May 14 '20 at 14:25
  • If you want it to be whatever button caused `randomValue` to be called, just add `document.getElementById('getir').value = this.value;` somewhere in there (before the loop if you want the value before randomization, after the loop if you want the random value). – Heretic Monkey May 14 '20 at 14:29
  • No I want to set the value of `numbers` to the value of `getir` – AlkimYalcin May 14 '20 at 15:03
  • Did u check it @HereticMonkey? – AlkimYalcin May 15 '20 at 05:49
  • Please don't edit your question in such a way as to invalidate answers others have spent time working on. It is very disrespectful of their volunteer work. If you have a different question, related to this question, please ask a new question. If you've already solved it, you can answer it yourself but adding an answer. See https://stackoverflow.com/help/self-answer for more information. Thank you for respecting the rules of this site. – Heretic Monkey May 15 '20 at 12:08

2 Answers2

0

Numbers are randomizing this is fine.I need to change it like 0-9 numbers should not repeat when i click the buttons

Picking numbers from a random distribution doesn't work because each draw is independent and you have a very small population thus you get duplicates.

Rather than picking random numbers, what you want is to shuffle a fixed list of numbers (0 to 9 here). Here's a quick proof of concept applied to your keypad (using this Fisher-Yates implementation):

const buttons = document.querySelectorAll("input");
const numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

for (let i = 0, len = buttons.length; i < len; i++) {
  buttons[i].value = numbers[i];
  buttons[i].addEventListener("click", shuffleKeypad);
}

function shuffleKeypad() {
  shuffle(numbers);
  for (let i = 0, len = buttons.length; i < len; i++) {
    buttons[i].value = numbers[i];
  }
}

/**
 * Shuffles array in place.
 * @param {Array} a items An array containing the items.
 */
function shuffle(a) {
    var j, x, i;
    for (i = a.length - 1; i > 0; i--) {
        j = Math.floor(Math.random() * (i + 1));
        x = a[i];
        a[i] = a[j];
        a[j] = x;
    }
    return a;
}
<table>
  <tbody>
    <tr>
      <td><input type="button"></td>
      <td><input type="button"></td>
      <td><input type="button"></td>
    </tr>
    <tr>
      <td><input type="button"></td>
      <td><input type="button"></td>
      <td><input type="button"></td>
    </tr>
    <tr>
      <td><input type="button"></td>
      <td><input type="button"></td>
      <td><input type="button"></td>
    </tr>
    <tr>
      <td><input type="button"></td>
    </tr>
  </tbody>
</table>
Álvaro González
  • 142,137
  • 41
  • 261
  • 360
  • Thanks Âlvaro i used this method but not in array and it works fine.For now i only have to do set buttons value to textbox onClick or other functions.Edited post. – AlkimYalcin May 14 '20 at 14:19
  • While this is good advice, there are many, many duplicate questions explaining how to shuffle an array... – Heretic Monkey May 14 '20 at 14:26
  • @HereticMonkey I'm not explaining how to shuffle an array (in fact function `shuffle` is copied right from the link and the snippet is shown with illustration purposes). I'm explaining that rolling random numbers is not the solution to this specific problem. – Álvaro González May 14 '20 at 14:29
  • @AlkimYalcin Please don't mutate the question as you solve partial problems :_( – Álvaro González May 14 '20 at 14:31
  • I fixed the problem which "shuffle brings same numbers" right now it shuffles correctly. Yet i need to set them in textbox when I click the numbers it must be written on textbox. – AlkimYalcin May 14 '20 at 14:59
  • @HereticMonkey I've added a hopefully more useful explanation. Unfortunately the question has been edited and changed, rendering my answer useless. – Álvaro González May 14 '20 at 14:59
  • If I was you, I'd rollback the question. Edits that invalidate existing answers are invalid and should be rolled back. – Heretic Monkey May 14 '20 at 15:03
  • @HereticMonkey You're right. AlkimYalcin, Feel free to post a new question with your current doubts. Please add a runnable snippet to it. Thank you! – Álvaro González May 15 '20 at 07:32
0

SOLVED BY MYSELF IF ANYONE NEEDS ...

////////////////////////////////////////////////////////

function yazdir(yazi) {

    document.getElementById("getir").value += yazi;

    function Random() {
        return (Math.floor(Math.random() * 10));
    }

    var i;
    for (i = 0; i < 10; i++) {
        a = Random(); //a = 5

        var x = document.getElementById("s" + i); // 1 değeri için  x=1
        var y = document.getElementById("s" + a);
        var temp;

        temp = x.value; // x : 0
        x.value = y.value; // y :5
        y.value = temp; //temp :0
    }
}

function silYazi() {
    document.getElementById("getir").value = "";
}
<table>
  <tr>
    <input type="text" id="getir" value="" name="getir" disabled>
  </tr>
  <br>
  <tr>
    <input id="s1" type="button" class="numbers btn btn-outline-success mt-3" onclick="yazdir(value)" value="1">
    <input id="s2" type="button" class="numbers btn btn-outline-success mt-3 ml-4" onclick="yazdir(value)" value="2">
    <input id="s3" type="button" class="numbers btn btn-outline-success mt-3 ml-4" onclick="yazdir(value)" value="3">
  </tr>
  <br>
  <tr>
    <input id="s4" type="button" class="numbers btn btn-outline-success mt-3" onclick="yazdir(value)" value="4">
    <input id="s5" type="button" class="numbers btn btn-outline-success mt-3 ml-4" onclick="yazdir(value)" value="5">
    <input id="s6" type="button" class="numbers btn btn-outline-success mt-3 ml-4" onclick="yazdir(value)" value="6">
  </tr>
  <br>
  <tr>
    <input id="s7" type="button" class="numbers btn btn-outline-success mt-3" onclick="yazdir(value)" value="7">
    <input id="s8" type="button" class="numbers btn btn-outline-success mt-3 ml-4" onclick="yazdir(value)" value="8">
    <input id="s9" type="button" class="numbers btn btn-outline-success mt-3 ml-4" onclick="yazdir(value)" value="9">
  </tr>
  <br>
  <tr>
    <input id="s0" type="button" class="numbers btn btn-outline-success mt-3" onclick="yazdir(value)" value="0">
    <input id="btnSil" type="button" class="btn btn-outline-danger mt-3" onclick="silYazi();" value="Sil">
  </tr>
</table>