'Math drag & drop
English isnt my first language so sorry for bad formulation.
So at school im taking a coding course right now i have an assignment where i need to make a drag and drop math game, where it generates 6 random numbers and you need to sort them into the boxes in ascending order. When you have sorted the numbers you are supposed to press a button that lets you know how many right you have. Below you see my code so far
In short my question is how do i make a function that checks if the numbers in the different boxes are in ascending order
window.onload= oppstart;
function oppstart () {
document.getElementById("hovedside").onclick = tilbakeHside;
document.getElementById("historieside").onclick = tilbakeHisSide;
randNumb();
}
function tilbakeHside() {
window.location.href = "index.html";
}
function tilbakeHisSide() {
window.location.href = "fag1.html";
}
function allowDrop(ev) {
ev.preventDefault();
}
function drag(ev) {
ev.dataTransfer.setData("text", ev.target.id);
}
function drop(ev) {
ev.preventDefault();
var data = ev.dataTransfer.getData("text");
ev.target.appendChild(document.getElementById(data));
}
function randNumb() {
var rand1 = Math.trunc(Math.random()*1000000);
var rand2 = Math.trunc(Math.random()*1000000);
var rand3 = Math.trunc(Math.random()*1000000);
var rand4 = Math.trunc(Math.random()*1000000);
var rand5 = Math.trunc(Math.random()*1000000);
var rand6 = Math.trunc(Math.random()*1000000);
document.getElementById("drag1").innerHTML = rand1;
document.getElementById("drag2").innerHTML = rand2;
document.getElementById("drag3").innerHTML = rand3;
document.getElementById("drag4").innerHTML = rand4;
document.getElementById("drag5").innerHTML = rand5;
document.getElementById("drag6").innerHTML = rand6;
}
#hovedside, #historieside {
float: right;
background-color:transparent;
border-radius:8px;
border:1px solid #ffffff;
cursor:pointer;
color:#ffffff;
font-family:Arial;
font-size:20px;
}
#logo {
width: 15%;
float: left;
}
body {
align-items: center;
background-color: aqua;
}
#dropBox {
background-color: burlywood;
font-size: xx-large;
width: 850px;
height: 200px;
margin: auto;
display: flex;
align-items: center;
justify-content: center;
}
#dragBox {
background-color: cornsilk;
font-size: xx-large;
width: 850px;
height: 100px;
margin: auto;
display: flex;
align-items: center;
justify-content: center;
}
#div1, #div2, #div3, #div4, #div5, #div6 {
float: left;
width: 100px;
height: 100px;
margin: 10px;
padding: 10px;
border: 1px solid black;
}
<div id="top">
<button id="hovedside"> <h2> hovedside </h2> </button> <button id="historieside"><h2>Tilbake til Matematikk hovedside</h2></button>
<img id="logo" src="logo.png">
</div>
<br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/> <br/>
<div id="dropBox">
<div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<div id="div2" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<div id="div3" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<div id="div4" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<div id="div5" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<div id="div6" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
</div>
<br/> <br/> <br/> <br/>
<div id="dragBox">
<p draggable="true" ondragstart="drag(event)" id="drag1" width="88" height="31">11</p>
|
<p draggable="true" ondragstart="drag(event)" id="drag2" width="88" height="31">22</p>
|
<p draggable="true" ondragstart="drag(event)" id="drag3" width="88" height="31">33</p>
|
<p draggable="true" ondragstart="drag(event)" id="drag4" width="88" height="31">44</p>
|
<p draggable="true" ondragstart="drag(event)" id="drag5" width="88" height="31">55</p>
|
<p draggable="true" ondragstart="drag(event)" id="drag6" width="88" height="31">66</p>
</div>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
