Answer to Question #310309 in HTML/JavaScript Web Application for vicky

Question #310309

Find the First Value

Given an array myArray of positive integers, write a JS program to find the smallest integer divisible by 2 and 3.

input

the input will be a single line containing an myArray

output

the output should be a single line containing a number divisible by 2 and 3 or undefined.

input1

[51,18,15,12]

output1

12

input2

[41,29,17,19,31]

output2

undefined


"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() {

 let myArray = JSON.parse(readLine());

 /* Write your code here and log the output */

}




1
Expert's answer
2022-03-15T07:12:18-0400
"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() {
    let myArray = JSON.parse(readLine());
    /* Write your code here and log the output */
    let res = [];

    myArray.sort();

    myArray.map(item => {
        if (item % 2 == 0 && item % 3 == 0 && res.length < 1) {
            res.push(item);
        }
    })

    if (res.length > 0) {
        console.log(res[0]);
    } else {
        console.log('undefined');
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS