forked from rbk-org/rbk-toy-problems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathW4D2.js
56 lines (52 loc) · 1.79 KB
/
W4D2.js
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
//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 mate1=makemate('hala','salhab','Female',19,165)
var mate2=makemate('kholoud','mohtaseb','Female',38,160)
var mate3=makemate('qamar','jardat','Female',25,158)
var classmates=[mate1,mate2,mate3]
function makemate(fname,lname,gender,age,length){
return {
fname:fname,
lname:lname,
fullname:fname+" "+lname,
gender:gender,
age:age,
length:length
}
}
function displayFriend(mate){
return 'Full Name: '+mate.fullname+' Age: '+mate.age;
}
function addFriend(mate){
classmates.push(m)
}
function nbOfMale(){
var count=0;
for(var i=0;i<classmates.length;i++){
if(classmates[i].gender==='Male'){
count+=1
}
}
return 'There is '+count+'Males in the class .'
}
//Part2:
// Using recursion Write a JavaScript function to find the greatest common divisor (gcd) of two positive numbers.
// 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 sumation(num1,num2){
if(num2===0){
if(num1%2===0)
{return -1}
else {
return 0
}
} return num1-num2+sumation(num1,num2-1)
}
sumation(5,2)
sumation(6,2)