-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscriptBasics.js
More file actions
136 lines (110 loc) · 3.13 KB
/
scriptBasics.js
File metadata and controls
136 lines (110 loc) · 3.13 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
window.onload = function(){
function testMyFunction(expected, literal){
if(expected===literal){
return "Successful";
}else{
return "Not successful, Expected is " + expected + " Found is " + literal + "."
}
}
// 1. Return max of two
function MaxOfTwo(x,y){
if(x>y){
return x;
}else{
return y;
}
}
console.log(testMyFunction(9, MaxOfTwo(6,9)));
// 2. return max of three
function MaxOfThree(x,y,z){
if(x>y&&x>z){
return x;
}else if(y>x&&y>z){
return y;
}else{
return z;
}
}
console.log(testMyFunction(100, MaxOfThree(6,9,100)));
// 3. return true if is voule
function isVowel( x){
if( x.length==1 &&(x=='a' || x=='e'|| x=='i' || x=='o' || x=='u') ){
return true;
}else {
return false;
}
}
console.log(testMyFunction(true, isVowel('a')));
// 4 sum and multiply
function sum(x){
var sum=0;
var i=0;
for(i; i<x.length;i++){
sum=sum+x[i];
}
return sum
}
function multiply(x){
var mul=1;
var i=0;
for(i; i<x.length;i++){
mul=mul*x[i];
}
return mul;
}
var q1= [1,2,3,4]
console.log(testMyFunction(10, sum(q1)));
console.log(testMyFunction(24, multiply(q1)));
//5 reverse
function reverse(x){
if(x.length==1)
{return x;}
var rev="";
for(let i=x.length-1;i>=0;i--){
rev= rev +x[i];
}
return rev;
}
console.log(testMyFunction ("dlroW olleH", reverse("Hello World")));
//6. returning the length of the longest word from an array
function findLongestWord(arr){
let longest=arr[0].length;
for(let i=1; i<arr.length; i++){
if(longest<arr[i].length){
longest = arr[i].length;
}
}
return longest;
}
console.log((10, findLongestWord(["well", "come", "to", "the", "world", "of", "javascript" ])));
//7. taking an array of words and an integer i and returning the array of words that are longer than i
function filterLongWords(arr, i){
let newArr=[];
for(let j=0; j<arr.length; j++){
if(arr[j].length>i){
newArr.push(arr[j]);
}
}
return newArr;
}
console.log("Expected output of filtered long words of the given array is 'three, four, five, seven' " + filterLongWords([ "three", "four", "five", "seven" ], filterLongWords(["one", "two", "three", "four", "five", "six", "seven"], 3)));
//8. function modifing(){
const a = [1,3,5,3,3];
//a) multiply each element by 10;
const b = a.map(function(elem, i, array) {
return elem *10;
});
console.log("After multipling [1,3,5,3,3] by 10 is " +b);
//document.writeln(b.toString() + "<br/>");
//b) return array with all elements equal to 3;
const c = a.filter(function(elem, i, array){
return elem === 3;});
console.log("After filtering 3s from [1,3,5,3,3] is " +c);
//document.writeln(c.toString() + "<br/>");
//c) return the product of all elements;
const d = a.reduce(function(prevValue, elem, i, array){
return prevValue * elem;
});
console.log("The product of [1,3,5,3,3] is " +d);
//document.writeln(d.toString() + "<br/>");
}