Repository navigation
Expand file tree
/
Copy pathassignment1.ts
More file actions
79 lines (73 loc) · 2.7 KB
/
Copy pathassignment1.ts
File metadata and controls
79 lines (73 loc) · 2.7 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
//Write a program that uses filter to remove all negative numbers from an array of numbers
let array : number [] = [1,-2,3,-4,5,6,-7,8];
let updatedArray = array.filter
( (Number)=>{
if (Number >0)
{return true}
}
)
console.log(updatedArray)
console.log("new program start");
//Given an array of numbers [1, 2, 3, 4, 5], use the map method to create a new array where each number is multiplied by 2.
let array1 : number [] = [1,2,3,4,5];
let updatedArray1 = array.map(
(Num)=>{
{return 2*Num}
}
)
console.log("Array = ",array1)
console.log("UpupdatedArray = ",updatedArray1)
console.log("new program start");
// Given an array of strings ["apple", "banana", "cherry", "date", "grape"], use the filter method to create a new array containing only the fruits with more than 5 characters.
let array3: string []= ["apple", "banana", "cherry", "date", "grape"];
let updatedArray3 = array3.filter((num)=>
{if (num.length>5)
return true
})
console.log("Array = ",array3)
console.log("UpupdatedArray containing more than 5 characters = ",updatedArray3)
console.log("new program start");
//Given an array of numbers [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], use the map and filter methods together to create a new array containing the squares of even numbers.
let array4 : number [] = [ 1,2,3,4,5,6,7,8,9,10 ];
let evenArray = array4.filter(
(Num)=>{
if (Num%2===0)
{return 1}
}
)
let squareArray = evenArray.map ((Num)=>Num*Num )
console.log("Array = ",array4)
console.log("Square of even Numbers = ",squareArray)
console.log("new program start");
// Given an array of temperatures in Celsius [0, 10, 20, 30, 40], use the map method to create a new array where each temperature is converted to Fahrenheit using the formula (Celsius * 9/5) + 32.
let array5 : number [] = [0,10,20,30,40 ];
let ctoFaArray = array5.map(
(Num)=>{
return (Num * (9/5) +32)
}
)
console.log("Array = ",array5)
console.log(" Celsius to fahrenheit = ",ctoFaArray)
console.log("new program start");
//Given an array of numbers [3, 6, 9, 12, 15, 18], use the map and filter methods together to create a new array containing the doubled values of odd numbers.
let array6 : number [] = [ 3, 6, 9, 12, 15, 18 ];
let oddArray = array6.filter(
(Num)=>{
if (Num%2!=0)
return true
}
)
let doubleOdd = oddArray.map((Num)=>
{return Num*2}
)
console.log("Array = ",array6);
console.log("Odd Array = " , oddArray)
console.log("Double of odd = ",doubleOdd)
console.log("new program start");
//Given an array of names ["Alice", "Bob", "Charlie", "David", "Emily"], use the forEach method to log each name with an exclamation mark at the end, e.g., "Alice!".
let array7 : string [] = [ "Alice", "Bob", "Charlie", "David", "Emily" ];
array7.forEach(
(str)=>{
console.log(`${str}!`)
}
)