Snail Climdomain_7 Up a Wall

function main() { var depth = parseInt(readLine(), 10); //your code goes here i = 0; for (; depth > 0;) { i++; depth -= 7 if (depth > 0) { depth += 2 } } console.log(i); } Here ya go m8.

My answer is here. function main() { var depth = parseInt(readLine(), 10); //your code goes here var climb = 7; var slip = 2; var day = 0; for(workdone=0;workdone<=depth;) { day = day + 1; workdone=workdone+climb; if(workdone>=depth){ break; } workdone = workdone - slip; } console.log(day); }

For those interested, here's an explanation on why Ibrahim Matar's answer works. function main() { var depth = parseInt(readLine(), 10); for(dis = 7, day = 1; dis < depth; dis += 7, day++) { dis -= 2 } console.log(day); } A key part of understanding this lies in knowing the order in which a for loop is evaluated and executed. There are four parts of a for loop (I'll include the corresponding code from the answer in parentheses for clarity): - initialization (dis = 7, day = 1) - condition (dis < depth) - final-expression (dis += 7, day++) - statement (dis -= 2) Although they are presented in the order listed above, they actually run in this order: 1. initialization ("dis" is declared and given a value of 7, "day" is declared and given a value of 1) 2. condition (if "dis < depth" is true, the for loop will continue onto steps 3 and 4. If it is false, the for loop will stop immediately without executing steps 3 and 4) 3. statement ("dis" is decreased by 2) 4. final-expression ("dis is increased by 7, "day" is increased by 1) From here, the loop will run again, but it starts with step 2 ("dis < depth"), not the first one. This is evaluated, once again looking for either true or false. Going through it step by step. Let's say "depth = 11". First time through the loop: - dis is set to 7, day is set to 1 - dis (which is 7) < depth (which is 11) = true (the for loop continues to steps 3 and 4) - dis (which is 7) is decreased by 2, so dis is now 5 - dis (which is 5) is increased by 7, so dis is now 12; and day (which is 1) is increased by 1, so day is now 2 Second time through the loop: - dis (which is 12) < depth (which is 11) = false (the for loop does not continue to steps 3 and 4) - the for loop ends, console.log(day) is executed, and at this moment, day has a value of 2 With more space this could be explained further, but this is why extra conditionals, variables, while loops, and arrays aren't necessary here.

My noob code for(x=1; depth >0; x++){ depth -=7; if(depth<=0){ console.log(x); break; } depth +=2; if(depth<=0){ console.log(x); break; } }

console.log(Math.round(depth / 5))

var climb = 7; var slip = 2; var day = 0; for(workdone=0; workdone=depth;) { day = day + 1; workdone=workdone + climb; if(workdone = depth){ break; } workdone = workdone - slip; } console.log(day); }

function main() { var depth = parseInt(readLine(), 10); //your code goes here for (dis=7,day=1;dis<depth;dis+=7,day++){ dis=dis-2; } console.log (day) }

function main() { var depth = parseInt(readLine(), 10); //equals 42 in this example var x = 0; //milesDone var y = 0; //dayCount for(i=0 ; i < depth ;i ++){ if (x<depth) { x += 7; if (depth > x) { x -= 2; } y++ } else { console.log(y) break; } } }

thats my code: function main() { var depth = parseInt(readLine(), 10); //your code goes here var day = 0; var way = 0; while (way < depth) { way += 7; day++; if (way >= depth) { break; } way -= 2; } console.log(day); }

So I wasted a lot of time with the loop method, I decided to find a way to simplify the problem by exploring other options and this is what I came up with. function main() { var depth = parseInt(readLine(), 10); day = depth/5; console.log(Math.round(day)); } We know that the snail moves 7 and slides back 2 each night; so that is 5 per day. We know depth is determined by user input. So we divide depth by 5 and we get a number and decimal. Using the Math.round() function we eliminate our decimal and store the answer in the var day. Hope this helps.

function main() { var depth = parseInt(readLine(), 10); //your code goes here let data = []; /** Array contains the distance snails moves per days after an incremental +7 and decreamental -2 */ for (let i = 7; depth > 0 && i < depth + 7; i += 7) { if (i < depth) i -= 2; /**it only subtracts 2 as far my incremental value of +7 is less than or equal to input value */ data.push(i); }; console.log(data.length); /**this will output the length of data which will be the number of days */ } or function main() { var depth = parseInt(readLine(), 10); //your code goes here for ( i = 7, day = 1; i < depth; i += 7, day++) { if (i < depth) i -= 2; /**it only subtracts 2 as far my incremental value of +7 is less than or equal to input value */ }; console.log(day); /**this will output the length of data which will be the number of days */ }

----------------------------------- PASSED ALL TEST CASES ----------------------------------- // VARIABLES const climb = 7; const slip = -2; let day = 0; // SOLUTION for (progress = 0; progress <= depth;) { progress += climb; day +=1; if (progress >= depth) { break; } else { progress += slip; } } // OUTPUT console.log(day);

But why ? Initial 1 day/7 feet not 0 ?

function main() { var depth = parseInt(readLine(), 10); //your code goes here var day=parseInt(0); var ng=parseInt(2); var clday=parseInt(7); for(days=0;days<=depth;){ day=day+1; days=days+clday; if(days>=depth){ break; } days=days-ng; } console.log(day); } hooora!!!

function main() { var depth = parseInt(readLine(), 10); //your code goes here if(depth%5<=2) console.log(parseInt(depth/5)); else console.log(parseInt(depth/5)+1); }

function main() { var depth = parseInt(readLine(), 10); //your code goes here i = 0; for (; depth > 0;) { i++; depth -= 7 if (depth > 0) { depth += 2 } } console.log(i); }

function main() { var profundidade = parseInt(readLine(), 10); // seu código vai aqui, rsrs exclusivo pra Br for (i = 0; profundidade > 0; i++) { profundidade -= 7 if (profundidade > 0) { profundidade += 2 } } console.log(i); }

for (days = 0; depth > 0; days++){ depth -= 7 if (depth > 0) { depth += 2 } } console.log(days)

Vlad and that's correct or not ?

Hi all ! I dont understand why my solution doesn't work... Can you advise ? for(day=0,distance<depth, distance-=2) {distance+=7; day++} console.log(day) It looks like the loop keeps going when distance=depth. like for test 1 it gives one extra day but not for test 2 where it matches.

Snail Climdomain_7 Up a Wall

Source: https://www.sololearn.com/Discuss/2618113/the-snail-in-the-well-js

0 Response to "Snail Climdomain_7 Up a Wall"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel