In: Computer Science
Part 1
Recall from Chapter 1 that computer memory is comprised of individual bits of data. A bit (short for binary digit) can store one of only two values, commonly referred to as 0 and 1. However, using two bits, you can represent four different values through the bit patterns 00, 01, 10, and 11. With three bits, you can represent eight different values—via 000, 001, 010, 011, 100, 101, 110, and 111. In general, N bits of memory enable you to represent 2N different values.
Create a Web page named bits.html that contains a text box where the user can enter a number, call it N. At the click of a button, your page should compute and display 2N, the number of values that can be represented using the specified quantity of bits. For example, if the user entered 10 in the text box, the page would display the message:
With 10 bits, you can represent 1024 different values.
Once you have created your page, use it to determine the number of values that each of the following can represent (Test the following numbers to see if they give the correct results)
8 bits (1 byte)
16 bits (2 byte)
32 bits (4 bytes)
64 bits (8 bytes)
Part 2
Most lotteries select winning numbers by drawing numbered balls out of bins. For example, a typical Pick-4 lottery will utilize four bins, each containing balls with numbers starting at 0. If there are 10 balls to choose from in each of four bins, labeled 0 to 9, then 104 = 10,000 different number sequences can potentially be picked. Increasing the number of balls significantly increases the number of possible sequences, which significantly decreases a person’s odds of winning. For example, if there are 20 balls to choose from in each bin, labeled from 0 to 19, then 204 = 160,000 different number sequences could be selected.
Make a copy of the lucky1.html page from Figure 7.4 in your text and name it pick4.html. Then modify this new page so that it simulates a Pick-4 lottery. Your page should have one text box, where the user can enter the highest ball number (it is assumed that the lowest ball number is always 0). When a button is clicked, four random ball numbers should be selected and displayed in a message such as the following:
The Pick-4 winners are: 5-0-8-2
Part 3
Modify your pick4.html page from above so that it makes use of a function in the HEAD. Your function should contain the code previously assigned to the button, and have a name descriptive of the task it performs, such as GeneratePicks or PickNumbers. You should then modify the button’s ONCLICK attribute to call that function.
I have provided the properly commented code in both text and image format so you can easily copy the code as well as check for correct indentation. I have provided the output image of the code so you can easily cross-check for the correct output of the code. Have a nice and healthy day!!
Part 1
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Bits.html</title>
<!-- CSS only -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
<!-- Bootstrap related files -->
</head>
<div class='container m-4 p-4'>
<label for="binary">Enter a no.</label>
<input type="number" class="form-control" id="binary" placeholder="Enter number of bits">
<!-- Takes input here -->
<button type="button" class="btn btn-primary" onclick='calculate()'>Calculate</button>
<p class="font-weight-bold" id='output'></p>
</div>
<!-- Bootstrap related files -->
<!-- JS, Popper.js, and jQuery -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>
<script type='text/javascript'>
function calculate(){
var n = $('#binary').val();
// Get value of the number
var output = Math.pow(2,n);
// get total values by calculating 2^n
$('#output').html('With '+ n +' bits, you can represent '+ output +' different values.');
// Changing value of text
}
</script>
</body>
</html>
Part 2
<!doctype html>
<html lang="en">
<head>
<!-- CSS only -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
<meta charset="utf-8">
<title>pick4.html</title>
</head>
<div class='container m-4 p-4'>
<label for="highest">Enter highest ball number</label>
<input type="number" class="form-control" id="highest" placeholder="Enter highest ball number">
<!-- Takes input here -->
<button type="button" class="btn btn-primary" onclick='GENERATEPICKS()'>Pick Balls</button>
<p class="font-weight-bold" id='output'></p>
<!-- Prints output here -->
</div>
<!-- Bootstrap related files -->
<!-- JS, Popper.js, and jQuery -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>
<script type='text/javascript'>
function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}
// This function is inside head tag
function GENERATEPICKS(){
var n = parseInt($('#highest').val());
// Get value of the number as integer and not string
// If input is 9 then we want to check balls in range 0-9. So, we will find number by using getRandomInt(10)
// which will return a number in range of 0 to 9
var a = getRandomInt(n+1);
var b = getRandomInt(n+1);
var c = getRandomInt(n+1);
var d = getRandomInt(n+1);
// get total values by calculating 2^n
$('#output').html('The Pick-4 winners are: '+a+'-'+b+'-'+c+'-'+d);
// Changing value of text
}
</script>
</body>
</html>
Part-3
Same as above but functions are moved inside the head tag as mentioned in the question
<!doctype html>
<html lang="en">
<head>
<!-- CSS only -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
<!-- Bootstrap related files -->
<!-- JS, Popper.js, and jQuery -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>
<script type='text/javascript'>
function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}
// This function is inside head tag
function GENERATEPICKS(){
var n = parseInt($('#highest').val());
// Get value of the number as integer and not string
// If input is 9 then we want to check balls in range 0-9. So, we will find number by using getRandomInt(10)
// which will return a number in range of 0 to 9
var a = getRandomInt(n+1);
var b = getRandomInt(n+1);
var c = getRandomInt(n+1);
var d = getRandomInt(n+1);
// get total values by calculating 2^n
$('#output').html('The Pick-4 winners are: '+a+'-'+b+'-'+c+'-'+d);
// Changing value of text
}
</script>
<meta charset="utf-8">
<title>pick4.html</title>
</head>
<div class='container m-4 p-4'>
<label for="highest">Enter highest ball number</label>
<input type="number" class="form-control" id="highest" placeholder="Enter highest ball number">
<!-- Takes input here -->
<button type="button" class="btn btn-primary" onclick='GENERATEPICKS()'>Pick Balls</button>
<p class="font-weight-bold" id='output'></p>
<!-- Prints output here -->
</div>
</body>
</html>