-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule-1-programming-project.html
114 lines (82 loc) · 5.77 KB
/
module-1-programming-project.html
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>CMPS 260: Module 1 Programming Project</title>
<style>* { font-family: monospace; }</style>
<script>
// NOTE: You can enter both code and explanations. For an explanation, start
// the line with '//' which indicates a comment.
// NOTE: console.log prints text to the browser console that you can enable
// to see the output. It is a little bit more convenient than alert so
// we will use it most of the time.
// NOTE: Please review the following links regularly:
// https://it.pointpark.edu/tutorials/arrays-vs-objects/
// https://it.pointpark.edu/tutorials/no-prototype-vs-prototype/
// https://it.pointpark.edu/tutorials/implementation-vs-interface/
//---------//
// Project //
//---------//
console.log("Project");
// 1. Write a function that selects all elements from an array that are greater
// than n (this means n is a parameter for the function). Before you write
// the implementation of the function, first create some tests that will
// eventually show that your function works correctly.
// addded a new test comment for committed
function selectElementsGreaterThanNumber(array, n)
{
let result = array.filter(item => item > n);
return result;
}
console.log("Testing");
var someNums = [5,8,10,20,30,35,39,41,45,72];
var otherNums = selectElementsGreaterThanNumber(someNums, 30);
console.log("Function testing");
var expected = [35,39,41,45,72];
console.log("Results of the Function are: " + otherNums);
console.log("Results of Expected are: " + expected);
// test code
console.assert(otherNums, expected);
// 2. Write a function that checks if an array contains two numbers that sum
// to 5. Before you write the implementation of the function, first create
// some tests that will eventually show that your function works correctly.
newArray = [1,4,5,8,10,13];
function checkSumOfTwoEqual5(array)
{
for(var i = 0; i < array.length - 1; i++)
{
for(var j = i + 1; j < array.length; j++)
{
if( array[i] + array[j] == 5 )
return true;
}
}
return false;
}
console.assert(checkSumOfTwoEqual5([1,4,5,8,10,13]) == true);
console.assert(checkSumOfTwoEqual5([2,4,6,8]) == false);
// 3. The following text is from the all the Amendments in the Bill of Rights.
var text = "Congress shall make no law respecting an establishment of religion, or prohibiting the free exercise thereof; or abridging the freedom of speech, or of the press; or the right of the people peaceably to assemble, and to petition the Government for a redress of grievances. A well regulated Militia, being necessary to the security of a free State, the right of the people to keep and bear Arms, shall not be infringed. No Soldier shall, in time of peace be quartered in any house, without the consent of the Owner, nor in time of war, but in a manner to be prescribed by law. The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no Warrants shall issue, but upon probable cause, supported by Oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized. No person shall be held to answer for a capital, or otherwise infamous crime, unless on a presentment or indictment of a Grand Jury, except in cases arising in the land or naval forces, or in the Militia, when in actual service in time of War or public danger; nor shall any person be subject for the same offence to be twice put in jeopardy of life or limb; nor shall be compelled in any criminal case to be a witness against himself, nor be deprived of life, liberty, or property, without due process of law; nor shall private property be taken for public use, without just compensation. In all criminal prosecutions, the accused shall enjoy the right to a speedy and public trial, by an impartial jury of the State and district wherein the crime shall have been committed, which district shall have been previously ascertained by law, and to be informed of the nature and cause of the accusation; to be confronted with the witnesses against him; to have compulsory process for obtaining witnesses in his favor, and to have the Assistance of Counsel for his defence. In Suits at common law, where the value in controversy shall exceed twenty dollars, the right of trial by jury shall be preserved, and no fact tried by a jury, shall be otherwise re-examined in any Court of the United States, than according to the rules of the common law. Excessive bail shall not be required, nor excessive fines imposed, nor cruel and unusual punishments inflicted. The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people. The powers not delegated to the United States by the Constitution, nor prohibited by it to the States, are reserved to the States respectively, or to the people.";
// First we make the text lower case.
text = text.toLowerCase();
// Then we remove punctuation by using a regular expression.
text = text.replace(/[.,;]/g, "");
// And store the words into an array.
var textArray = text.split(" ");
// 4. Remove all words from the array of length less than or equal to 3. Or,
// similarly, keep all words of length 4 or greater.
textArray = textArray.filter(item => item.length >=4 );
// 5. Create a new array that contains all duplicate words that are left after
// removing the short words in step (1).
var duplicated = textArray.filter((item, index) => {
var count = textArray.filter((item1, index1) => item == item1 && index != index1).length;
return count > 0;
});
duplicated = duplicated.filter((item, i, ar) => ar.indexOf(item) === i);
console.log("Duplicated Words", duplicated);
</script>
</head>
<body>
See console!
</body>
</html>