# Basic Problems Solving in JavaScript

Feb 4th, 2023
855
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. //feet to inch converter problem
2. // so we know that 1 feet  = 12inch
3.
4. //solutions
5. function feetToInch(feet){
6.     const inch = 12;
7.     const totalInch = feet*inch;
8.
10.
11. }
12.
13. const calFeetToInch = feetToInch(6);
14. console.log(calFeetToInch);
15.
16. //centimeter to meter converter problem
17.
18. //we know 100cm = 1m
19.
20. //solution
21.
22. function centimeterToMeter(cm){
23.     const divider = 100;
24.     const meter =  cm/divider;
25.
26.     return meter;
27. }
28.
29. const calCmToMeter = centimeterToMeter(200);
30. console.log(calCmToMeter);
31.
32.
33. //problem is to get total amount of papers for book quantities
34.
35. //for book 1 reserved 100 papers
36. //for book 2 reserved 200 papers
37. //for book 3 reserved 300 papers
38. //add quantiity for each book through function and calculate total numbers of papers
39.
40.
41. // solution
42.
43. function paperRequirements(bookOneQt,bookTwoQt,bookThreeQt){
44.     const bookOneReservedPaper =100;
45.     const bookTwoReservedPaper =200;
46.     const bookThreeReservedPaper =300;
47.
48.     const totalNumberOfPapers = (bookOneReservedPaper*bookOneQt)+(bookTwoReservedPaper*bookTwoQt)+(bookThreeReservedPaper*bookThreeQt);
49.
50.
52.
53. }
54.
55. const numberOfPapers = paperRequirements(5,6,8);
56.
57. console.log('Number of papaers requrired: ',numberOfPapers);
58.
59.
60. //find your best friend / find the biggest name from array
61.
62. //solution
63. const friends = ['Ajmal','Shahin','Javed','Rajib Islam','Miraz','Soumick Chattergee'];
64.
65. function bestFriend(arr){
66.     let biggestName =  arr[0];
67.
68.     for (var i =0; i<arr.length;i++){
69.         const name = arr[i];
70.         if(name.length > biggestName.length){
71.             biggestName =  name;
72.         }
73.     }
74.
75.     return biggestName;
76. }
77.
78. const myBestFriend = bestFriend(friends);
79.
80. console.log(myBestFriend);
81.
82. // extract only the positive numbers from the array
83. // break when it finds a negative number
84.
85. //soultion
86.
87. function onlyPositive(arr){
88.     const positiveNumbers = [];
89.     for(var i = 0;i<arr.length;i++){
90.
91.         const number =  arr[i];
92.
93.         if(number < 0){
94.             break;
95.         }else{
96.             positiveNumbers.push(number);
97.         }
98.
99.     }
100.     return positiveNumbers;
101. }
102.
103. const myNumbers =  [5,6,8,9,15,16,-18,20,25];
104.
105. console.log('positive numbers ',onlyPositive(myNumbers));
106.
107.
108.
109. // write a fibonacci programme
110. // fibonacci calculation always depends on previous two values
111. // n = n[i-1]+n[i-2]
112.
113. function fib(numbers){
114.
115.     //initial value
116.     let number = 0;
117.
118.     for (var i =2; i<25; i++){
119.
120.         number =  numbers[i-1]+numbers[i-2];
121.
122.         numbers.push(number);
123.
124.     }
125.
126.     return numbers;
127.
128. }
129.
130. const fibNumbers =  fib([0,1]);
131.
132. console.log(fibNumbers);
Tags: