|
| 1 | +Question. |
| 2 | +Python, Ruby or JavaScript Write a function that takes an array of integers and returns that array rotated by N positions using JavaScript. |
| 3 | +For example, if N=2, given the input array [1, 2, 3, 4, 5, 6] |
| 4 | +the function should return [5, 6, 1, 2, 3, 4] |
| 5 | + |
| 6 | +Answer:- |
| 7 | +var array = [1, 2, 3, 4, 5, 6]; |
| 8 | +var N = 2; |
| 9 | +function rotateArray(array, N) { |
| 10 | + newArray = array.slice(array.length - N).concat(array.slice(0, array.length - N)); |
| 11 | + return newArray; |
| 12 | +} |
| 13 | +rotateArray(array, N); |
| 14 | + |
| 15 | +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| 16 | +--------------------------------------------------------------------------------------------------------------------------- |
| 17 | + |
| 18 | +Question. |
| 19 | +In JavaScript , answer if the following expressions result in true or false (and explain your |
| 20 | +answer) |
| 21 | +a. "0" == 0 // true or false? |
| 22 | +b. "" == 0 // true or false? |
| 23 | +c. "" == "0" // true or false? |
| 24 | + |
| 25 | +Answer:- |
| 26 | +console.log("0" == 0); |
| 27 | +//In this case, leftval/leftoprnd is String and rightval/rightoprnd is Number Jvascript check Equality(operand string convert into Number type) ToNumber(0) == 0 |
| 28 | +console.log("" == 0); |
| 29 | +//In this case, leftval/leftoprnd is String and rightval/rightoprnd is Number Jvascript check Equality(operand "" convert into Number type) ToNumber(0) == 0 |
| 30 | +console.log("" == "0"); |
| 31 | +//In this case, both operands are String type and both are not same valued so will return as false; |
| 32 | + |
| 33 | +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| 34 | +--------------------------------------------------------------------------------------------------------------------------- |
| 35 | + |
| 36 | +Question. |
| 37 | +Most languages have a built in sort method that will sort an array of strings alphabetically. Demonstrate how to sort an array of strings by the length of each string, shortest strings first. |
| 38 | +Hint: clean, small code wins. |
| 39 | + |
| 40 | +Answer:- |
| 41 | +arr = [] |
| 42 | +arr[0] = "a"; |
| 43 | +arr[1] = "abcdef"; |
| 44 | +arr[2] = "abc"; |
| 45 | +arr[3] = "ab"; |
| 46 | +arr[4] = "abcde"; |
| 47 | +arr[5] = "abcd"; |
| 48 | +arr.sort(function(array1, array2){ |
| 49 | + return array1.length - array2.length; |
| 50 | +}); |
| 51 | + |
| 52 | +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| 53 | +--------------------------------------------------------------------------------------------------------------------------- |
| 54 | + |
| 55 | +Question. |
| 56 | +function weird(x) { |
| 57 | +var tmp = 3; |
| 58 | +return function(y) { |
| 59 | +return x + y + ++tmp; |
| 60 | +} |
| 61 | +} |
| 62 | +var funny = weird(2); |
| 63 | +var final_answer = funny(10); |
| 64 | +What is the value of final_answer at the end of this snippet? Please explain your answer |
| 65 | + |
| 66 | +Answer:- |
| 67 | +function weird(x) { //x parameter is 2 |
| 68 | + var tmp = 3; |
| 69 | + return function(y) { |
| 70 | + return x + y + ++tmp; //returns 2 + 10 + 4(incremented value 3+1) = 16 |
| 71 | + } |
| 72 | +} |
| 73 | +var funny = weird(2); |
| 74 | +var final_answer = funny(10); |
| 75 | + |
| 76 | +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| 77 | +--------------------------------------------------------------------------------------------------------------------------- |
| 78 | + |
| 79 | +Question. |
| 80 | +Consider the following pattern: |
| 81 | +A → D M → P X → A |
| 82 | +a → d m → p x → a |
| 83 | +Now, write a program to solve the following message using Python, JavaScript or Ruby. |
| 84 | +Vrphwklqj phdqlqjixo |
| 85 | +Hint: The answer is something meaningful. |
| 86 | + |
| 87 | +Answer:- |
| 88 | +function convertString(string) { |
| 89 | + for (var i = 0; i < string.length; i++) { |
| 90 | + var asc_code = string.charCodeAt(i); |
| 91 | + var conv_code; |
| 92 | + if ((asc_code >= 65 && asc_code <= 67) || (asc_code >= 97 && asc_code <= 99)) { |
| 93 | + conv_code = asc_code + 23; |
| 94 | + } else if ((asc_code >= 68 && asc_code <= 90) || (asc_code >= 100 && asc_code <= 122)) { |
| 95 | + conv_code = asc_code - 3; |
| 96 | + } else if (asc_code == 32 ) { |
| 97 | + conv_code = asc_code; |
| 98 | + } |
| 99 | + else |
| 100 | + conv_code = asc_code; |
| 101 | + |
| 102 | + document.write(String.fromCharCode(conv_code)); |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +convertString("Vrphwklqj phdqlqjixo"); |
| 107 | + |
| 108 | + |
| 109 | +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| 110 | +--------------------------------------------------------------------------------------------------------------------------- |
| 111 | + |
| 112 | +Question. |
| 113 | +Use the following paragraphs (in italics below) as input to the program you wrote for Question 5. |
| 114 | +It should output a meaningful question. Write a program to solve that question. |
| 115 | +Zulwh d surjudp (lq Sbwkrq, MdydVfulsw ru Uxeb) wr srsxodwh dqg wkhq vruw d |
| 116 | +udqgrpob glvwulexwhg olvw ri 1 ploolrq lqwhjhuv, hdfk lqwhjhu kdylqj d ydoxh >= 1 dqg <= |
| 117 | +100 zlwkrxw xvlqj dqb exlowlq/hawhuqdo oleudub/ixqfwlrq iru vruwlqj. |
| 118 | +Brxu surjudp vkrxog fduhixoob frqvlghu wkh lqsxw dqg frph xs zlwk wkh prvw hiilflhqw |
| 119 | +vruwlqj vroxwlrq brx fdq wklqn ri. Surylgh wkh vsdfh dqg wlph frpsohalwb ri brxu dojrulwkp |
| 120 | + |
| 121 | + |
| 122 | +convertString(`Zulwh d surjudp (lq Sbwkrq, MdydVfulsw ru Uxeb) wr srsxodwh dqg wkhq vruw d |
| 123 | +udqgrpob glvwulexwhg olvw ri 1 ploolrq lqwhjhuv, hdfk lqwhjhu kdylqj d ydoxh >= 1 dqg <= |
| 124 | +100 zlwkrxw xvlqj dqb exlowlq/hawhuqdo oleudub/ixqfwlrq iru vruwlqj. |
| 125 | +Brxu surjudp vkrxog fduhixoob frqvlghu wkh lqsxw dqg frph xs zlwk wkh prvw hiilflhqw |
| 126 | +vruwlqj vroxwlrq brx fdq wklqn ri. Surylgh wkh vsdfh dqg wlph frpsohalwb ri brxu dojrulwkp`); |
| 127 | + |
| 128 | + |
| 129 | +Answer: |
| 130 | +var randomList = []; |
| 131 | +for (var i = 0; i < 100; i++) { |
| 132 | + randomList.push(Math.round(Math.random() * 999999)) |
| 133 | +} |
| 134 | + |
| 135 | +function bubbleSort(randomListArray) { |
| 136 | + var nextLast = randomListArray.length - 1, holder, swappingOccured, index, nextIndex; |
| 137 | + randomListArray.some(function() { |
| 138 | + swappingOccured = false; |
| 139 | + for (index = 0; index < nextLast; index += 1) { |
| 140 | + nextIndex = index + 1; |
| 141 | + if (randomListArray[index] > randomListArray[nextIndex]) { |
| 142 | + holder = randomListArray[nextIndex]; |
| 143 | + randomListArray[nextIndex] = randomListArray[index]; |
| 144 | + randomListArray[index] = holder; |
| 145 | + swappingOccured = true; |
| 146 | + } |
| 147 | + } |
| 148 | + if (!swappingOccured) { |
| 149 | + return true; |
| 150 | + } |
| 151 | + return false; |
| 152 | + }); |
| 153 | +} |
| 154 | + |
| 155 | +bubbleSort(randomList); |
| 156 | + |
| 157 | +console.log(randomList); |
| 158 | + |
| 159 | + |
| 160 | +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| 161 | +--------------------------------------------------------------------------------------------------------------------------- |
| 162 | + |
| 163 | +Question. |
| 164 | +Write a simple program that reads a line from the keyboard and outputs the same line where |
| 165 | +every word is reversed. A word is defined as a continuous sequence of alphanumeric characters |
| 166 | +or hyphen (‘-’). For instance, if the input is |
| 167 | +"We are at Google Solutions! Their email-id is [email protected]" |
| 168 | +the output should be |
| 169 | +"eW era ta elgooG snoituloS! riehT di-liame si [email protected]" |
| 170 | +HINT: Read the problem and the example carefully before starting |
| 171 | + |
| 172 | +Answer:- |
| 173 | + |
| 174 | +var _string = "We are at Google Solutions! Their email-id is [email protected]"; |
| 175 | +var fnReverseString = function(_string){ |
| 176 | + return _string.replace(/[a-zA-Z0-9-_]+/gm, function(_response) { |
| 177 | + return _response.split("").reverse().join(""); |
| 178 | + }); |
| 179 | +}; |
| 180 | +console.log(fnReverseString(_string)); |
0 commit comments