-
Notifications
You must be signed in to change notification settings - Fork 0
/
codehw2
52 lines (40 loc) · 2 KB
/
codehw2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<html>
<body>
<h1>Challenge: ISBN Validation</h1>
<?php
/*CHALLENGE: Given the constraints of ISBN numbers, write a function that can return TRUE if a number is a valid ISBN and FALSE otherwise. Test out your function with several valid and invalid 10-digit ISBNs including ones where the last digit is written as X.*/
$isbn = "0345391802";
#I wanted to distinguish / make call-able each individual digit of the isbn
$first = substr($isbn, 0, 1);
$second= substr($isbn, 1, 1);
$third = substr($isbn, 2, 1);
$fourth = substr($isbn, 3, 1);
$fifth = substr($isbn, 4, 1);
$sixth = substr($isbn, 5, 1);
$seventh = substr($isbn, 6, 1);
$eighth= substr($isbn, 7, 1);
$ninth = substr($isbn, 8, 1);
$tenth = substr ($isbn, 9, 1);
if ($tenth == "X")
{$tenth == 10;}
/*ISBN CONSTRAINTS: 1. Obtain the sum of 10 times the first digit, 9 times the second digit, 8 times the third digit... all the way till you add 1 times the last digit.
2. Divide the sum by 11, if the sum leaves no remainder when divided by 11 the code is a valid ISBN.*/
function verifyISBN($first, $second, $third, $fourth, $fifth, $sixth, $seventh, $eighth, $ninth, $tenth)
{$sumISBN =
(10 * $first) + (9 * $second) + (8 * $third) + (7 * $fourth) + (6 * $fifth) +
(5 * $sixth) + (4 * $seventh) + (3 * $eighth) + (2 * $ninth) + (1 * $tenth);
$Result = $SumISBN / 11;
return $Result;}
#calling the function
$ISBNtotal = verifyISBN($first, $second, $third, $fourth, $fifth, $sixth, $seventh, $eighth, $ninth, $tenth);
?>
<h1>Challenge: Coin Toss</h1>
<?php
/*The mt_rand() function returns a random value; optionally you may enter a mix and max number to be returned. Documentation is available at http://www.w3schools.com/php/func_math_mt_rand.asp
Create a PHP page that simulates a series of random coin tosses for 1, 3, 5, 7, and 9 flips.
b. Create a loop that will toss the coin repeatedly until you have flipped exactly two heads in a row. Stop the loop and print out to the page the number of tosses this took*/
echo(mt_rand(1,2). "<br>"););
if
?>
</body>
</html>