So basically, everything works except that the program eats the first digit sometimes, and sometimes does not. Examples:
1. Example
- INPUT: 221
- EXPECTED OUTPUT: 2
- ACTUAL OUTPUT: 1
2. Example
- INPUT: 122
- EXPECTED OUTPUT: 2
- ACTUAL OUTPUT: 2
3. Example
- INPUT: 422
- EXPECTED OUTPUT: 3
- ACTUAL OUTPUT: 3
Example 2 works because the program eats the first digit, which is not an even digit, so the count works either way. Whereas for Example 1, the program eats one of the 2s and thus, the program outputs 1, and not 2, since one of them is gone and the count didn't get it. And in the third example, it suddenly works. I am sure it has something to do with the math, but I just do not get it.
index.php
<html>
<head>
<meta charset="UTF-8">
<style>
body {
font-family: Arial;
font-size: 13px;
}
</style>
</head>
<body>
<form method="post" action="result.php">
<center>
<label for="Input">Въведете число:</label>
<input type="number" id="Input" name="Input"><br><br>
<input type="submit" value="Провери!">
</center>
</form>
</body>
</html>
result.php
<html>
<meta charset="UTF-8">
<body>
<?php
$input = $_REQUEST["Input"];
$count = 0;
for ($i = 0; $i < $input; $i )
{
$x = $input % 10;
if ($x % 2 == 0)
{
$count ;
}
$input = (int)($input / 10);
}
?>
Числото <?php echo $_POST["Input"]; ?> съдържа <?php echo $count ?> четни цифри.
</body>
</html>
CodePudding user response:
result.php
<html>
<meta charset="UTF-8">
<body>
<?php
$input = $_REQUEST["Input"];
$count = 0;
for ($i = 0; $i < $input; $i )
{
$x = $input % 10;
if ($x % 2 == 0)
{
$count ;
}
$input = (int)($input / 10);
}
?>
Числото <?php echo $_POST["Input"]; ?> съдържа <?php echo $count ?> четни цифри.
</body>
</html>
Change to:
<html>
<meta charset="UTF-8">
<body>
<?php
$input = str_split($_REQUEST["Input"]);
$input_length = count($input);
$count = 0;
for($i = 0; $i < $input_length; $i){
if($input[$i] % 2 === 0){
$count;
}
}
?>
Числото <?php echo $_POST["Input"]; ?> съдържа <?php echo $count ?> четни цифри.
</body>
</html>
CodePudding user response:
I built another for-loop for this:
$input = mb_str_split($_REQUEST["Input"]);
$evennum = "";
for($i = 0;$i < count($input);$i )
{
if($input[$i]%2==0)
{
$evennum .= $input[$i];
}
}