Replacing Array Item
input
output
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
}
"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);
}
Comments
Leave a comment