S
S
Sergey Vasenin2020-04-18 23:14:53
IT education
Sergey Vasenin, 2020-04-18 23:14:53

Please help with learning JS on stepic. How to solve this problem?

Please help me with an example. On the stepic platform, the admins are silent, and I have been racking my brains for the 2nd day, I don’t need to decide for me, but just tell me how to solve the problem like this . - number of the previous robot of this model (number). You need to return the full name of the new robot as a string. To do this: Increment the variable n by 1 and add it to str , and write the result to the variable result, which is then returned from the function with return 2: Roboto 1






Sample Output 2:

Roboto2
Sample Input 3:

r2d 1
Sample Output 3:

r2d2

function modify(str,n){
 var result;
 // Здесь ваш код
 return result;   
}


it turns out that it is necessary to do so

function modify(str,n){
 var result;
 var = n++;
str+n;
 return result;   
}


or how?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
hzzzzl, 2020-04-19
@emin313

var = n++;
^ there is an error,
firstly this is the wrong syntax, n++this is a self-sufficient expression that will increase n
secondly

According to the language specification, the difference between pre- and post-increment is that when the expression is evaluated, the result is the old value in one case, and the new value of the variable being incremented in the other

that is, n++ (post-increment) will first return n , and then add to n + 1
n = 1;
n1 = n++;
console.log(n, n1) // 2, 1

thirdly, you do not return anything from the function at all
function modify(str,n){
 var result;
 var = n++;  // ?
 str+n;  // ? ок это никуда не сохраняется и ничего не делает
 return result;   // чему равен result?
}

but you have to returnстроку + (номер + 1)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question