forked from Ayaabuyousef/rbk-toy-problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathW2D2.js
More file actions
65 lines (58 loc) · 2.34 KB
/
W2D2.js
File metadata and controls
65 lines (58 loc) · 2.34 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
//1-using the console calculate the average age of the follwing ages [13,14,13,15,16,17,19,13,16,15].
var avg=0
for(i=13;i<=15;i++){
avg+=i
}
avg=avg/10
// 2-using the console calculate your age in seconds.
var myAge=21*365*24*60*60
// 3- Write a function identity that takes one parameter and returns that input value.
// Calling your function should result in:
// identity("hello world"); ==> "hello world"
// identity(500); ==> 500
function identity(par){
return par
}
// 4- Write a function convertTo that takes a string and a number as parameters.
// If the string input is "cm", then the function should convert the 2nd argument into centimeters by multiplying it to 2.54 and returning that value.
// If the string input is "in", then the function should convert the 2nd argument into inches by dividing it by 2.54.
// Calling your function should result in something like:
// convertTo('cm', 100); ==> 254
// convertTo('in', 50.8); ==> 20
function convertTo(units, num) {
// write your code here
if (units==='cm'){
return num*2.54
}
return num/2.54
}
//4- Write a function dogsIWouldPet that takes an item (string),
//and returns a sentence stating dogs you would pet in comparison to the item (see sample call below).
// Calling your function should result in:
// dogsIWouldPet("ottoman"); //"I would pet dogs no bigger than an ottoman"
// dogsIWouldPet("small horse"); //"I would pet dogs no bigger than an small horse"
// dogsIWouldPet("Terrier"); //"I would pet dogs no bigger than an Terrier"
// Bonus (extra): If your function were passed in a string "I do not like dogs",
//you can have your function return "I would not pet dogs".
function dogsIWouldPet (str){
if(str==="ottoman"){
return "I would pet dogs no bigger than an ottoman"
}
if(str==="small horse"){
return "I would pet dogs no bigger than an small horse"
}
if(str==="Terrier"){
return "I would pet dogs no bigger than an Terrier"
}
if(str==="I do not like dogs"){
return"I would not pet dogs"
}
}
// 5- Write a function convertToKilometers that takes a number of miles passed in as parameter,
// and returns that number multiplied by 1.60934 (an accepted approximation of 1 mile in kilometers).
// Calling your function should result in:
// convertToKilometers(50); ==> 80.467
// convertToKilometers(361); ==> 580.973
function convertToKilometers(num){
return num*1.60934
}