-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
49 lines (45 loc) · 1.91 KB
/
test.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
var planet1 = "Earth"
var planet2= "Mars"
var planet3= "jupiter"
console.log(planet1,"is nearer than",planet2,"to",planet3)
var x=10
console.log(++x)
// comparison operators also work in opposite direction
if (11==x){
console.log("ksjdb ",x)
}
//ternary operator = "?"
var res
res = (11==x) ? 11 : "other"
console.log(res,"-")
while(x-- > 0 ){
console.log(x)
}
var planets=["Earth" , "Uranus" , "neptune" , "saturn","sdvf","sgfsfvv"]
console.log(planets)
var morePlanets = ["pluto","mars","asdf","qwert"]
console.log("**slice " + planets.slice(-3))
//Accessing last 3 elements
//Slice doesn't delete the selected chunk, splice does
console.log(planets.reverse())
//Reverse of an array. It does permanent damage to an array
var pl=morePlanets
// A deep copy hppened here where the array pl is copied from the memory where morePlanets is saved.
morePlanets[1]="*****"
console.log("this is pl-- " + pl + " -- this is morePlanets -- " + morePlanets , "\nDeep copy" , "...\n\n")
/*To do shallow copy , where an array is copied from the contents of another array, we can use slice method. when we copy a variable, shallow copy automatically happens
In deep copy, the array itself is copied from memory instance.
In shallow copy, the values inside the array is copied to the other array.
shallow copy can also be done like this (in CS6)-- pl=[...morePlanets]
*/
pl = [...morePlanets]
pl[1]="----"
console.log("this is pl -- " + pl + " -- this is morePlanets -- " + morePlanets , "\nShallow copy" , "...\n\n")
//Merge two arrays planets and morePlanets
planets.reverse()
var solarSys = planets.concat(morePlanets)
console.log("Merged two planets list " +planets + " --and-- " + morePlanets + " to build a solar system >>> " + solarSys)
//Lots of concat
solarSys = planets.concat(morePlanets, pl )
//this can also be done like this-- solarSys = planets.concat(morePlanets).concat(pl)
console.log("\nBig reoccurring planets solar system >>> " + solarSys)