To detect the pressed arrow key in JavaScript, the code is as follows;Example Live Demo Document body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .sample { font-size: 18px; font-weight: 500; } Detecting the pressed arrow key Press any arrow key to know which arrow key has been pressed let fillEle = document.querySelector(".sample"); document.body.addEventListener("keydown", (event) => { switch (event.keyCode) { case 37: fillEle.innerHTML = "Left key pressed"; break; case 38: fillEle.innerHTML = "Up key pressed"; break; case 39: fillEle.innerHTML = "Right key pressed"; break; case 40: fillEle.innerHTML = "Down key pressed"; break; } }); OutputOn pressing any of the arrow key −
Following is the code for detecting a mobile browser in JavaScript −Example Live Demo Document body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .sample { font-size: 18px; font-weight: 500; } Detecting a mobile browser CLICK HERE Click on the above button to see if it is a mobile device or not let fillEle = document.querySelector(".sample"); let date = new Date(); let primitiveDef = date[Symbol.toPrimitive]("default"); let primitiveNum = date[Symbol.toPrimitive]("number"); document.querySelector(".Btn").addEventListener("click", () => { var checkMobile = /iPhone|iPad|iPod|Android/i.test(navigator.userAgent); if (checkMobile) { fillEle.innerHTML = "This ... Read More
To get an enumerator that iterates through Collection, the code is as follows −Example Live Demousing System; using System.Collections.ObjectModel; public class Demo { public static void Main(){ Collection col = new Collection(); col.Add("Andy"); col.Add("Kevin"); col.Add("John"); col.Add("Kevin"); col.Add("Mary"); col.Add("Katie"); col.Add("Barry"); col.Add("Nathan"); col.Add("Mark"); Console.WriteLine("Count of elements = "+ col.Count); Console.WriteLine("Iterating through the collection..."); var enumerator = col.GetEnumerator(); while (enumerator.MoveNext()) { ... Read More
Suppose there is a special square room with mirrors on each of the four walls. In each corner except the southwest corner, there are receptors. These are numbered as 0, 1, and 2. Now the square room has walls of length p, and a laser ray from the southwest corner first meets the east wall at a distance q from the 0th receptor. We have to find the number of the receptor that the ray meets first.So if p = 2, and q = 1, then the case will be like −So the output will be 2, as the ray ... Read More
Suppose we have a balanced parentheses string S, we have to compute the score of the string based on the following rule −The () has score 1AB has score A + B, where A and B are two balanced parentheses strings.(A) has score 2 * A, where A is a balanced parentheses string.So if the input is like “(()(()))”, then the output will be 6.To solve this, we will follow these steps −ans := 0, define a stack stfor i in range 0 to size of string Sif S[i] is opening parentheses, then insert -1 into stackotherwiseif top of stack ... Read More
Suppose there are N cars that are going to the same destination along a one lane road. The destination is ‘target’ miles away. Now each car i has a constant speed value speed[i] (in miles per hour), and initial position is position[i] miles towards the target along the road.A car can never pass another car ahead of it, but it can catch up to it, and drive bumper to bumper at the same speed. Here the distance between these two cars is ignored - they are assumed to have the same position. A car fleet is some non-empty set of ... Read More
Suppose we have a string S of lowercase letters, and an integer array shifts. The shift of a letter means the next letter in the alphabet, for z, it will be a. Now for each shifts[i] = x, we want to shift the first i+1 letters of S, x times. We have to find the final string after all such shifts to S are applied. So if the string is “abc” and shifts = [3, 5, 9], then after shifting the first 1 letter of S by 3, will have “dbc”, shifting first two letters of S by 5, we ... Read More
Suppose Rima has a hand of cards, given as an array of integers. Now she wants to shuffle the cards into groups so that each group is size W, and consists of W consecutive cards. We have to check whether it is possible or not.So if the cards are [1, 2, 3, 6, 2, 3, 4, 7, 8], and W = 3, then the answer will be true, as she can rearrange them like [1, 2, 3], [2, 3, 4], [6, 7, 8]To solve this, we will follow these steps −Define a map m, and store frequency of each element ... Read More
Many different types of data container can get mixed up in python. A list can have elements each of which is a tuple. In this article we will take such a list and find the frequency of element in the tuples which are themselves elements of a list.Using count and mapWe apply a lambda function to count through each of the first element in the tuples present in the list. Then apply a map function to arrive at the total count of the element we are searching for.Example Live Demo# initializing list of tuples listA = [('Apple', 'Mon'), ('Banana', 'Tue'), ('Apple', ... Read More
Consider any (contiguous) subarray B (of A) a called mountain if the following properties hold −size of B >= 3There exists some 0 < i < B.length - 1 such that B[0] < B[1] < ... B[i-1] < B[i] > B[i+1] > ... > B[B.length - 1]Suppose we have an array A of integers; we have to find the length of the longest mountain. We have to return 0 if there is no mountain. So if the input is like [2, 1, 4, 7, 3, 2, 5], then the result will be 5. So the largest mountain will be [1, ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP