Simple code not working?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
12th Sep 2020, 10:31 AM
thatkidayo
thatkidayo - avatar
8 Answers
+ 4
You've used the return statement inside the loop. As soon as the control enters the loop, it does the calculation and meets the return statement, so the function ultimately returns the value, and further execution of the loop is discontinued. Move the return statement outside of the loop. Happy coding <^_^>
12th Sep 2020, 10:40 AM
Charitra
Charitra - avatar
12th Sep 2020, 11:12 AM
thatkidayo
thatkidayo - avatar
+ 1
thatkidayo According to your new question, i++ and ++i, both have same work, that is to increment i by 1. The difference here is, if you use an expression with these, they'll behave differently, otherwise, they will have same meaning like i=i+1. Consider these //case 1 var i = 5; console.log(i++); console.log(i); //case 2 var i = 5; console.log(++i); console.log(i); See the output of these two cases.. you'll get what I'm saying. i++ will give the previous value of i in the statement where it is used, and then i+1 from next statement onwards. And ++i will give i+1 immediately. You can experiment by using them in different arithmetic expressions.
12th Sep 2020, 11:18 AM
Charitra
Charitra - avatar
+ 1
oops i asked the wrong question😭 please check it again🙏🙏🙏🙏 https://code.sololearn.com/WSPtUOs8GFeH/?ref=app
12th Sep 2020, 11:21 AM
thatkidayo
thatkidayo - avatar
+ 1
thatkidayo Just remove the i++ from the for statement. It will look like this for(i=0;i<number-1;) this is because, you are incrementing i twice in a single run.. that's why its not showing all values. Happy Coding <^_^> off topic - (https://www.youtube.com/c/everythingcomputerized)
12th Sep 2020, 11:24 AM
Charitra
Charitra - avatar
+ 1
thanks a lot🙏 ill checkout the youtube channel
12th Sep 2020, 11:27 AM
thatkidayo
thatkidayo - avatar
0
thatkidayo thank you☺
12th Sep 2020, 11:28 AM
Charitra
Charitra - avatar
0
for(i=0;i<number;i++){ let num=i+1; arr[i]=num; Use this
14th Sep 2020, 7:14 AM
Salaruddin Jalal
Salaruddin Jalal - avatar