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

Question #310300

Replacing Array Item

input

  • the first line of input contains an array myArray
  • the second line of input contains a targetItem
  • the third line of input contains a replaceItem

output

  • the output should be an array containing the replaceItem.

input1

[1,2,3,"four",5,6]

"four"

4

output1

[1,2,3,4,5,6]


input2

[22,44,88,352,352]

352

176

output2

[22,44,88,176,352]


"use strict";


process.stdin.resume();

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


let inputString = "";

let currentLine = 0;

////

/////

function main() {

 let myArray = JSON.parse(readLine().replace(/'/g, '"'));

 let targetItem = JSON.parse(readLine().replace(/'/g, '"'));

 let replaceItem = JSON.parse(readLine().replace(/'/g, '"'));


  Write your code here and log the output

}

















1
Expert's answer
2022-03-15T07:12:06-0400
"use strict";

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

let inputString = "";
let currentLine = 0;

function main() {
    let myArray = JSON.parse(readLine().replace(/'/g, '"'));
    let targetItem = JSON.parse(readLine().replace(/'/g, '"'));
    let replaceItem = JSON.parse(readLine().replace(/'/g, '"'));
    // Write your code here and log the output
    let t = false;

    for(let i = 0; i < myArray.length; i++) {
        if(myArray[i] == targetItem && !t) {
            myArray[i] = replaceItem;
            t = true;
        }
    }

    console.log(myArray);
}

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