Hey there saijmen,
I changed some things and got it to work as far as it being right or wrong based on the first four pieces that are drag/dropped:
Code:
//creates array for what is in targetboxes
var targetContents = new Array();
//tests array for correct values
function checkSolved(a, b, c, d) {
var aright = false, bright = false, cright = false, dright = false;
aright = (targetContents[0] === a) ? true : false;
bright = (targetContents[1] === b) ? true : false;
cright = (targetContents[2] === c) ? true : false;
dright = (targetContents[3] === d) ? true : false;
return (aright && bright && cright && dright) ? true : false;
}
//tests targetContents for water symbol solution
function isSolved() {
if (targetContents.length === 4) {
if (checkSolved("square1", "square15", "square5", "square20")) {
window.open("http://www.webxpertz.net");
}
else {
alert("Wrong!");
}
}
}
//Prevents default actions by browsers
function allowDrop(e){
e.preventDefault();
}
//stores the image id when dragging
function drag(e){
e.dataTransfer.setData("Text", e.target.id);
}
//Appends the HTML section with image data when dropped
function drop(e){
e.preventDefault();
var data=e.dataTransfer.getData("Text");
e.target.appendChild(document.getElementById(data));
}
//Changes target area and returns picture id
function dropTarget1(e){
e.preventDefault();
var data=e.dataTransfer.getData("Text");
e.target.appendChild(document.getElementById(data));
targetContents[0] = document.getElementById(data).id;
isSolved();
}
//Changes target area and returns picture id
function dropTarget2(e){
e.preventDefault();
var data=e.dataTransfer.getData("Text");
e.target.appendChild(document.getElementById(data));
targetContents[1] = document.getElementById(data).id;
isSolved();
}
//Changes target area and returns picture id
function dropTarget3(e){
e.preventDefault();
var data=e.dataTransfer.getData("Text");
e.target.appendChild(document.getElementById(data));
targetContents[2] = document.getElementById(data).id;
isSolved();
}
//Changes target area and returns picture id
function dropTarget4(e){
e.preventDefault();
var data=e.dataTransfer.getData("Text");
e.target.appendChild(document.getElementById(data));
targetContents [3] = document.getElementById(data).id;
isSolved();
}
Notes on the old code to help you with the reasoning for the changes:
while (targetContents.length == 4)
This line would never be executed where it was - the length of the array was not 4 when the script ran, and as written the script would never go back to it to check again.
if (targetContents = ["square1", "square15", "square5", "square20"])
A couple of things: the single = will reassign the listed values to the targetContents array rather than checking it. Also, I don't think two arrays can be equal, I think you have to check each value.
targetContents[0] = getElementById(data);
A similar line was in each of the dropTarget functions. As written, this assigns the element object to the variable, when you wanted the element's id. I fixed this in the code by making it getElementById(data).id, though this may not be the most elegant solution... Also, those functions didn't test to see if the puzzle was solved after a drop. I added a function call (which fixes the while loop problem and array issue) to test after each of the drops.
Your challenge: There still needs to be a couple of tweaks. There probably needs to be a way to reset the pieces when the answer is incorrect. I can keep dropping pieces but they do not show up after I get 4 in there. Also, the if (targetContents.length === 4) bit only works properly if the tiles are dropped in order, we need it to check when the length is 4, but also need to check that all four values are defined.
Hope this helps get you toward a completed script...
Bookmarks