forked from Ayaabuyousef/rbk-toy-problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathW4D2.js
More file actions
93 lines (69 loc) · 2.28 KB
/
W4D2.js
File metadata and controls
93 lines (69 loc) · 2.28 KB
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
//Part1:
// create a data model to represent your classmates
// -think of different attributes of your classmates? what do all of them have ?
// -create a factory function.
// -create an array to hold the classmates that you created and what you created to it .
// -write a function called displayFriend that takes a mate as an argument and returns the important information in a readable way.
// -write a function called addFriend that takes a mate as an argument and add it to you classMates arraya.
// -calculate the number of male friends that your class have by writing a function called nbOfMale.
var friend1 = classmatesFactory("Hiba", 24, "Tulkarem", "female")
var friend2 = classmatesFactory("Rami", 21 , "Nablus", "male")
var classmates = [friend1,friend2]
var malesnum = 0
function MyClassmates (){
var instance = {}
instance.classmatesFactory = classmatesFactory
instance.displayFriend = displayFriend
instance.addFriend = addFriend
instance.nbOfMale = nbOfMale
return instance
}
function classmatesFactory (name ,age , homeTown, gender){
var classmate = {}
classmate.name = name
classmate.age = age
classmate.homeTown = homeTown
classmate.gender = gender
return classmate
}
function displayFriend (array){
var str = ""
for (var i = 0 ; i < array.length ; i++){
str += "My friend name is " + array[i].name + " and he/she lives in "+ array[i].homeTown + "/n"
}
return str
}
function addFriend (mate){
classmates.push(mate)
}
function nbOfMale (classmatesArr){
for (var i = 0; i <classmatesArr; i++) {
if( classmatesArr[i].grnder === "male"){
malesnum += 1
}
}
return malesnum
}
//Part2:
// Using recursion Write a JavaScript function to find the greatest common divisor (gcd) of two positive numbers.
function gcd ( num1,num2,num22=num2){
if (num1 < num2){
var hold = num1
num1 = num2
num2 = num1
}
if (num1 % num2 ===0 && num22 % num2 ===0){
return num2
}
return gcd(num1, --num2 , num22)
}
// Write a function called sum that accepts two numbers as parameters, and sum them together but without suming them
// togther directly
//you can only add one at each summation, you'll need to use recursion in this.
function sum (num1, num2, x=0){
if(num2 <= x)
return num1
x = x + 1
num1 += 1
return sum(num1,num2,x)
}