A string containing a number and a "Z" separated by a space is given. You want to add numbers in a string in turn. If you get a "Z" at this point, it means subtracting the number that you just added. Given a string s consisting of a number and "Z", try completing the solution function to return the value obtained by the awkward.
My solution is to spin the iterations and put them in a new array called answer, and then subtract them when Z comes out and make answer. Use sum to solve the answer.
function solution(s) {
let Array =s.split(" ")
let answer=[];
for(let i=0; i<Array.length; i++){
answer.push(Array[i])
if(Array[i]==='Z'){
answer.pop()
}
}
return answer.reduce((a,b)=>a+b,0)
}
I just need to fix two places.
Let's check the status quo first. What do I get when I run the following?
console.log(solution("1 1 Z 1"));
The expected output is an integer called 2
. In reality, however, the string "0111"
is outputted. Well, there are two main problems.
You'll be able to do it yourself from here. Fighting!
© 2024 OneMinuteCode. All rights reserved.