|
| 1 | +// question 22 |
| 2 | +//reverse a array |
| 3 | +let arr=[1,2,3,4,5] |
| 4 | +console.log("this is the initial array"+arr) |
| 5 | +let first=0 |
| 6 | +let last =arr.length-1 |
| 7 | +while(first<last){ |
| 8 | + let temp=arr[first] |
| 9 | + arr[first]=arr[last] |
| 10 | + arr[last]=temp |
| 11 | + first++ |
| 12 | + last-- |
| 13 | +} |
| 14 | +console.log("this is the updated array"+arr) |
| 15 | + |
| 16 | +//question 23 |
| 17 | +//reverse a string |
| 18 | +// reversing a string in the javascript is not as easy as reversing the array in the javascript |
| 19 | +//because the string is immutable in nature that means that we cannot reverse the string directly by converting that |
| 20 | +//string into the array and then after you join it that is how the javascript is reversing the string |
| 21 | + |
| 22 | +//1st method (by using methods) |
| 23 | +let sidha = "JavaScript"; |
| 24 | +let ulta = sidha.split("").reverse().join() |
| 25 | +console.log(ulta) |
| 26 | + |
| 27 | +//2nd method(manual one) |
| 28 | +let karnahai="" |
| 29 | +for(let i=sidha.length-1;i>=0;i--){ |
| 30 | + karnahai+=sidha[i]+"," |
| 31 | + |
| 32 | +} |
| 33 | +console.log(karnahai) |
| 34 | + |
| 35 | +//question 24 |
| 36 | +// a function that will merge two arrays and return the result as a new array |
| 37 | +// in order to this question there are basically three ways in which i will solve the |
| 38 | +//problem |
| 39 | +let arr1=[1,2,3] |
| 40 | +let arr2=[4,5,6] |
| 41 | +//1st method with the use of spread operator very easy |
| 42 | +let res=[...arr1,...arr2] |
| 43 | +console.log(res) |
| 44 | +//second by using the concat method |
| 45 | +let jodo= arr1.concat(arr2) |
| 46 | +console.log(jodo) |
| 47 | +//manual method |
| 48 | +function jodna_hai(arr1, arr2){ |
| 49 | +let dono=[] |
| 50 | +for(let element of arr1){ |
| 51 | + dono.push(element) |
| 52 | +} |
| 53 | +for(let element of arr2){ |
| 54 | + dono.push(element) |
| 55 | +} |
| 56 | +return dono |
| 57 | +} |
| 58 | +let milgya=jodna_hai(arr1,arr2) |
| 59 | +console.log(milgya) |
| 60 | + |
| 61 | +//question 25 |
| 62 | +// Create a function that will receive two arrays and will |
| 63 | +// return an array with elements that are in the first array but not in the second |
| 64 | +// this will use the .inlude() method |
| 65 | +function ekarray(arr1,arr2){ |
| 66 | + let result=[] |
| 67 | + for(let el of arr1){ |
| 68 | + if(!arr2.includes(el)){ |
| 69 | + result.push(el) |
| 70 | + } |
| 71 | + }return result |
| 72 | +} |
| 73 | +console.log(ekarray(arr1,arr2)) |
| 74 | + |
0 commit comments