-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex4.js
167 lines (146 loc) · 3.59 KB
/
index4.js
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
// stack vs heap
//primitive data stored in stack and refrence data stored in heap
const name='John'
const age=30
const person=
{
name:'Jonathan',
age:31
}
let newName=name;
console.log(name,newName);//John John
newName='Johnsonny'
console.log(name, newName);// John Johnsonny
//TYPE CONVERSION
let number= '100'
let convStore= parseInt(number)
console.log(convStore, typeof convStore);//100 number
// convert string to float
let amount='99.5'
amount=parseFloat(amount);
console.log(amount, typeof amount)// 99.5
//convert float to int
amount= parseInt(amount);
console.log( amount ,typeof amount);// 99 number
amount= Boolean(amount)
console.log(amount, typeof amount)// true boolean
amount='hello'
console.log(parseInt(amount));//NaN
typeof amount// 'number'
Math.sqrt(-11)//NaN
typeof Math.sqrt(-11)//'number'
console.log(1+NaN);//NaN
console.log(undefined + undefined);//NaN
let num12= 'foo' *3// NaN
typeof num12// 'number'
num12= 'lolo' +22// lolo22
typeof num12// 'string'
// operators
//arthemtic operators-- +,-,/,*,%
//concatenation
let x= 'Mahima'+ ' ' + 'Bhardwaj';
console.log(x);// Mahima Bhardwaj
// exponent
let power= 2 ** 4
console.log(power);
//increment
let x1 =2
x1++;//2
console.log(x);//3
++x1//4
//decrement
x1--// 4
console.log(x1);//3
--x1;// 2
console.log(--x1+x1++);//2
// assigment
let x22=4
x22**=2;//16// power operator
//Type coehersion- javascript internall changes the type
var num=12+'11';
console.log(num,typeof num);// 1211, string
//type conversion
let num2= 22
console.log(string(num2));//22
typeof num2// string
// CONVERT string into number
let x12=5 +'5'
x12= 5 + number('5')
console.log(x12);//10
typeof x12/// number
// strings
let y;
let name='Kiara'
let age= 21
y='Hello , my name is' + name + 'and I am' + age + 'years old';
console.log(y);//Hello , my name is John and I am 21 years old
let y1= `Hello , my name is ${name} and I am ${age} years old`
console.log(y1);
let s11='hello world'
// to create string into string object
let x= new String('hello world')// string object
typeof x//object
// access value by key
x= s11[1];//e
x=s11.__proto__; // it will show all properties of string
// properties and methods of string
s11.toLowerCase();// hello world
s11.toUpperCase();// HELLO WORLD
s11.charAt(3);// l
s11.indexOf('l');// 2
s11.lastIndexOf('l')//9
s11.substring(0,4);// hell
s11.substring(4);//o world
// if statement
let soup="chicken noodle soup"
let reply;
if(soup)
{
reply=`Here your order of ${soup}`;//
}
else
{
console.log('no soup');
}
console.log(reply);//Here your order of chicken noodle soup
//if else statement
let name1='John Hammond'
if(name1 ==='Raj Saxena')
{
console.log(`My name is ${name1}`);
}
else
{
console.log('this is not my name' );// this is not my name
}
let str= 'hello world'
str.slice(-10,-1)
str.substring(2,4)//ll
str.replace('w','H')// 'hello Horld'
//Create a var
let sum, difference, product, quotient, remainder;
let x= Math.floor(Math.random()*20+1)
let y= Math.floor(Math.random()*20+1)
sum= x+y
difference=x-y
product= x*y
quotient=x/y
remainder= x%y
console.log(sum)
console.log(difference)
console.log(product)
console.log(quotient)
console.log(remainder)
str.includes('ello')//true
str.valueOf()//'hello Horld
str.replace('h', 'H')// Hello Horld
// caplitalize a single character in string
let str11= 'hello world'
// sol1
let y12= str11.charAt(0).toUpperCase()+ str11.substring(1)// Hello world
//sol2
let z1= str11[0].toUpperCase()+ str11.substring(1);// Hello world
// sol 3
let p= `${str11[0].toUpperCase()}${str11.substring(1)}`// Hello world
// sol 4
let k= str11.replace('h','H')