Finding Out the Harshad Number in JavaScript

AmitDiwan
Updated on 19-Aug-2020 06:42:30

591 Views

Harshad numbers are those numbers which are exactly divisible by the sum of their digits. Like the number 126, it is completely divisible by 1+2+6 = 9.All single digit numbers are harshad numbers.Harshad numbers often exist in consecutive clusters like [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [110, 111, 112], [1010, 1011, 1012].Our job is to write a function that takes in a Number as input checks whether it’s a harshad number or not, if not then returns -1 otherwise it returns the length of streak of consecutive harshad cluster.For example −harshadNum(1014) = harshadNum(1015) = harshadNum(1016) = ... Read More

Create Random Number Between a Range in JavaScript

AmitDiwan
Updated on 19-Aug-2020 06:39:28

259 Views

Our job is to create a function, say createRandom, that takes in two argument and returns a pseudorandom number between the range (max exclusive).The code for the function will be −Exampleconst min = 3; const max = 9; const createRandom = (min, max) => {    const diff = max - min;    const random = Math.random();    return Math.floor((random * diff) + min); } console.log(createRandom(min, max));Understanding the code −We take the difference of max and minWe create a random numberThen we multiply the diff and random to produce random number between 0 and diffThen we add min to it ... Read More

Exclude Certain Values from Randomly Generated Array in JavaScript

AmitDiwan
Updated on 19-Aug-2020 06:38:09

1K+ Views

We have to create a function that takes in 2 arguments: an integer and an array of integers. First argument denotes the length of array we have to return and the second argument contains the elements that should not be present in our return array. Actually, we need an array of random numbers between 0 to 100 but it should not include any element that’s present in the argument array.Note − No two numbers should be duplicate.Let’s call our function generateRandom(). The code for this would be −Exampleconst absentArray = [44, 65, 5, 34, 87, 42, 8, 76, 21, 33]; ... Read More

Create Pagination Text in Android Using Kotlin

Azhar
Updated on 18-Aug-2020 12:35:49

368 Views

This example demonstrates how to create pagination text in Android using Kotlin.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.                     Step 3 − Add the following code to src/MainActivity.ktimport android.graphics.Color import android.graphics.Typeface import android.os.Bundle import android.text.Html import android.text.Spannable import android.text.SpannableString import android.text.TextUtils import android.text.style.ForegroundColorSpan import android.text.style.RelativeSizeSpan import android.text.style.StyleSpan import android.view.ViewTreeObserver import android.widget.Button import android.widget.TextView import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() {    lateinit var ... Read More

Implement Long Click Listener on Android ListView using Kotlin

Azhar
Updated on 18-Aug-2020 12:30:38

1K+ Views

This example demonstrates how to implement a long click listener on an Android listview using Kotlin.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new projectStep 2 − Add the following code to res/layout/activity_main.xml.     Step 3 − Add the following code to src/MainActivity.ktimport android.os.Bundle import android.widget.AdapterView.OnItemLongClickListener import android.widget.ArrayAdapter import android.widget.ListView import android.widget.Toast import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() {    var mobileArray = arrayOf("Android", "IPhone", "WindowsMobile", "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X")    override fun onCreate(savedInstanceState: Bundle?) {       ... Read More

Make GridLayout Fit Screen Size in Android Using Kotlin

Azhar
Updated on 18-Aug-2020 12:28:43

647 Views

This example demonstrates how to make a GridLayout fit screen size in Android using Kotlin.Step 1 − Create a new project in a new projectStep 2 − Add the following code to res/layout/activity_main.xml.                                                                 Step 3 − Add the following code to src/MainActivity.ktimport android.os.Bundle import android.view.Gravity import android.widget.GridLayout import android.widget.ImageView import android.widget.TableLayout import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() {    override fun onCreate(savedInstanceState: Bundle?) {       ... Read More

Set Input Type for Android EditText Using Kotlin

Azhar
Updated on 18-Aug-2020 12:25:31

1K+ Views

This example demonstrates how to set the input type for an Android EditText programmatically using Kotlin.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.             Step 3 − Add the following code to src/MainActivity.ktimport androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.text.InputType import android.view.View import android.widget.EditText class MainActivity : AppCompatActivity() {    private lateinit var editText: EditText    override fun onCreate(savedInstanceState: Bundle?) {       super.onCreate(savedInstanceState)       setContentView(R.layout.activity_main)   ... Read More

Bring Activity to Foreground in Kotlin

Azhar
Updated on 18-Aug-2020 12:23:32

603 Views

This example demonstrates how to bring an activity to the foreground i.e. top of stack using Kotlin.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new projectStep 2 − Add the following code to res/layout/activity_main.xml.     Step 3 − Add the following code to src/MainActivity.ktimport android.content.Intent import android.os.Bundle import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() {    override fun onCreate(savedInstanceState: Bundle?) {       super.onCreate(savedInstanceState)       setContentView(R.layout.activity_main)       title = "KotlinApp"       val i = Intent(this, NewActivity::class.java) ... Read More

Use Search Functionality in Custom ListView in Android Using Kotlin

Azhar
Updated on 18-Aug-2020 12:20:54

768 Views

This example demonstrates how to use search functionality in custom listview in Android using kotlinStep 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.         Step 3 − Add the following code to src/MainActivity.ktimport android.os.Bundle import android.text.Editable import android.text.TextWatcher import android.widget.ArrayAdapter import android.widget.EditText import android.widget.ListView import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() {    lateinit var listView: ListView    var months: ArrayList = ArrayList()    var arrayAdapter: ArrayAdapter? = null    lateinit var ... Read More

Send HTML Email using Android App with Kotlin

Azhar
Updated on 18-Aug-2020 12:18:23

895 Views

This example demonstrates how to send HTML email using Android App using Kotlin.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.         Step 3 − Add the following code to src/MainActivity.ktimport android.content.Intent import android.net.Uri import android.os.Bundle import android.text.Html import android.view.View import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() {    override fun onCreate(savedInstanceState: Bundle?) {       super.onCreate(savedInstanceState)       setContentView(R.layout.activity_main)       title = "KotlinApp"    }    fun ... Read More

Advertisements