Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
183 lines (132 loc) · 3.63 KB
/
Copy pathscript.js
File metadata and controls
183 lines (132 loc) · 3.63 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//This is an alert
//Single Line Comment
/*
Multi Line Comment
JS Introduction
*/
//dynamically typed language
//variables - 8 bytes
let score = 0; //initialization
//assignment
// score = 10.5 //1010
// score = 'no score'
const pi = 3.14;
//console.log("score is " + score);
/*
Operators
---------
Arithmetic Operators: + - * ** / % ++ --
Assignment Operators: = += -= *= /= %= **=
Comparison Operators: == === != !== > < >= <= ?:
Logical Operators: && || !
Bitwise Operators: & | ~ ^ << >> >>>
*/
//user input
// let name = prompt("whats your name?")
// console.log('hi ',name)
// let tickets = prompt('how many tickets do you want?')
// console.log("you should pay ", Number(tickets)*20)
// let a = prompt("Enter a number")
// console.log(Number(a)+10)
//String - Group of Characters
let str1 = "Strive not to be a success, ";
let str2 = "but rather to be a value";
/*
Partitioning String
slice(start, end)
substring(start, end)
substr(start, length)
*/
// Template Literals
// Introduced in 2015 with ECMAScript6 or ES6
let firstName = "Ramya";
let lastName = "Murali";
let city = "Chennai";
console.log(firstName + " " + lastName + " lives in " + city);
//string interpolation
console.log(`${firstName} ${lastName} lives in ${city}`);
//multiline string
let msg = `happy
birthday`;
console.log(msg)
//single and double quotes in string
msg = "cat's name is toto";
msg = `cat's name is "toto"`;
console.log(msg)
//arrays
let num = 10;
let name = "vidhya";
let cities = ["Chennai", "Madurai", "Trichy"];
let marks = [78, 56, 67, 54, 98];
//length - total elements in the array
console.log(marks.length);
//access 2nd element from start - index starts with 0.
console.log(cities[1]);
//access 3rd element in array
console.log(cities[3]);
//last element
console.log(marks[marks.length - 1]);
//mix of int and string
let arr = [5, 6, 7, "a", "abc", [3, 4]];
console.log(arr);
console.log(arr[5][1]);
//2d array
let matrix = [
[3, 4, 5],
[4, 5, 7],
[6, 7, 8],
];
console.log(matrix[2][1]);
//array methods
let array = ["a", "b", "c", "d", "e"];
//push - add element to the end and returns new length
array.push("f");
console.log(array);
//pop - removes element from the end and
//returns removed value
console.log(array.pop());
//shift - removes element from start of the array
//returns removed value
console.log(array.shift());
console.log(array);
//unshift - adds element to the start of the array
//returns new length
console.log(array.unshift("a"));
console.log(array);
//delete
// delete array[2]
// console.log(array[2])
//splice
//1st parameter - starting index
//2nd parameter - no. of elements to be deleted from starting index
//3rd(or more) parameter - values to be inserted from starting index
array.splice(2, 2); //deletes 2 elements starting at index 2
console.log(array);
array.splice(1, 1, "m"); // replace - deletes element at index 1 and inserts 'm'
console.log(array);
array.splice(1, 2, "x", "y"); //deletes elements at position 1 and 2 and inserts 'x','y'
console.log(array);
array.splice(1, 0, "b");
console.log(array);
//slice(starting index, ending index)
//ending index not included
console.log(array.slice(1, 2));
//reverse
array.reverse();
console.log(array);
//join - converts array to string
let str = array.join();
console.log(str);
//split - converts string to array
let str3 = "g,t,r,e";
let arr3 = str3.split(",");
console.log(arr3);
//concat and spread operator
let firstArr = [1, 2, 3];
let secondArr = [4, 5, 6];
let joinedArr = [firstArr,secondArr]
console.log(joinedArr)
joinedArr = firstArr.concat(secondArr)
console.log(joinedArr)
let joined = [...firstArr,...secondArr]
console.log(joined)