JS Program to log the value located at Given Index

Access the values of an array

Given an array. myArray = [25, false, “green”, “apple”]
Write a Js program to log the value located at the given index.

Input
The first line of input will contain a whole number (index).

Output
The output should be a single line containing the value located at the given index of the array.

Code:

"use strict";

process.stdin.resume();
process.stdin.setEncoding("utf-8");

let inputString = "";
let currentLine = 0;

process.stdin.on("data", (inputStdin) => {
  inputString += inputStdin;
});

process.stdin.on("end", (_) => {
  inputString = inputString
    .trim()
    .split("\n")
    .map((str) => str.trim());

  main();
});

function readLine() {
  return inputString[currentLine++];
}

function main() {
  // index
  let index = parseInt(readLine());
  // myArray
  let myArray = [25, false, "green", "apple"];
    
  console.log(myArray[index]);
 
}

Sample Input
0

Sample Output
25

Related Posts

Time Converter Using JavaScript | Simple JS Projects for Beginners

Time Converter Today, let’s build a Time Converter by applying the basic java script concepts. Refer to the below image. Instructions: The HTML input element for entering the number…

JavaScript Program to Create & Log an Array

Create & Log an Array Write a JS program to log an array with the given values.‘Orange’, 25, 100, true, 33.58. OutputThe output should be a single…

Traffic Signal Light using Javascript | JS Projects for Beginners with Code

Traffic Signal Light Today, let’s build a Traffic Light by applying the basic Javascript concepts. Refer to the below image. Instructions: The HTML button elements should have the ids stopButton, readyButton,…

Build Simple Color Picker using JavaScript | JS Projects with Code

let’s build a Color Picker by applying the basic JavaScript concepts. Refer to the below image. Instructions: The HTML container element that is wrapping all the HTML…

Leave a Reply

Your email address will not be published.