开发者

Javascript not working after for loop is done

开发者 https://www.devze.com 2023-04-05 05:06 出处:网络
I can\'t manage to make the line in the code with all the asterisks to work. Can anyone help? The function works fine, it is just the document.write(\'This will not print\'); that doesn\'t actually wo

I can't manage to make the line in the code with all the asterisks to work. Can anyone help? The function works fine, it is just the document.write('This will not print'); that doesn't actually work. What is the problem?

//UPDATE//

First of all, thanks for the quick replies. Now I will post the full code. As you can see the variable ii is declared at the body.

FULL CODE

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script t开发者_如何学编程ype="text/javascript">
<!--
function runThis() {
var fruta01 = document.getElementById('fruta').value;
var color01 = document.getElementById('color').value;
var problemo = true;
    for (newCount = 0; newCount<=ii; newCount++){
            if (storeArray[newCount].match(fruta01) && storeArray[newCount].match(color01)){
            document.write(storeArray[newCount]+'<br/>');
            problemo = false;
            };
    };
document.write('This doesnt print')
};

//-->
</script>
</head>

<body>
<script type="text/javascript">
<!--

//Matriz total
var storeArray = new Array ()

//Numero de objetos
var ii = 0;

//Color de los objetos
var colorCounter = 0
var color = new Array ()
color[0] = 'verde';
color[1] = 'amarillo';

//tipo de objetos
var objectCounter = 0
var object = new Array ()
object[0] = 'limones';
object[1] = 'platanos';
object[2] = 'manzanas';



for (ii; ii<8; ii++) {
storeArray[ii] = 'Tengo ' + ii + ' ' + object[objectCounter] +' de color ' + color[colorCounter];

        if (objectCounter != 2) {
        ++objectCounter
        }else{
        objectCounter = 0   
        }
        if (colorCounter != 1) {
        ++colorCounter
        }else{
        colorCounter = 0    
        }
} 

//print array so that you may see what you can choose...
for(var i=0;i<storeArray.length;i++){
document.write(storeArray[i]+"<br>");
}

//-->
</script>

<form>
Que fruta buscas? <input type="text" size="10" id='fruta'/><br />
De que color? <input type="text" size="10" id='color'/><br />
<input type="submit" value="Submit!" onclick="runThis()"/>
</form>

</body>
</html>


EDIT

The problem is in your for loop

for (newCount = 0; newCount <= ii; newCount++ )

Should be

for (newCount = 0; newCount < ii; newCount++ )

You don't have to put a semicolon after function, and 'if' and 'for' loops.

Here is a better version of your code

function runThis () {
    var fruta01 = document.getElementById('fruta').value;
    var color01 = document.getElementById('color').value;
    var problemo = true;

    for (newCount = 0; newCount <= ii; newCount++ )
    {
        if (storeArray[newCount].match(fruta01) && storeArray[newCount].match(color01) )
        {
            document.write(storeArray[newCount]+'<br/>');
            problemo = false;
        }
    }

    document.write('This will not print'); //**************************
}

Just use console.log() or a simple alert if you want to check just that.


Thats because the loop fails

You never set ii anywhere.

Check your console to make sure there are no javascript errors.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号