+ 27
The Snail in the Well ? JS
The snail climbs up 7 feet each day and slips back 2 feet each night. How many days will it take the snail to get out of a well with the given depth? Sample Input: 31 Sample Output: 6 Explanation: Let's break down the distance the snail covers each day: Day 1: 7-2=5 Day 2: 5+7-2=10 Day 3: 10+7-2=15 Day 4: 15+7-2=20 Day 5: 20+7-2=25 Day 6: 25+7=32 So, on Day 6 the snail will reach 32 feet and get out of the well at day, without slipping back that night. Hint: You can use a loop to calculate the distance the snail covers each day, and break the loop when it reaches the desired distance.
142 Respostas
+ 162
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.
+ 81
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.
+ 48
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);
}
+ 38
console.log(Math.round(depth / 5))
+ 33
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;
}
}
+ 15
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.
+ 12
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)
}
+ 9
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);
}
+ 6
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;
}
}
}
+ 5
-----------------------------------
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);
+ 3
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);
}
+ 3
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);
}
+ 2
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 */
}
+ 2
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);
}
+ 2
function main() {
var depth = parseInt(readLine(), 10);
//your code goes here
var sum = 0;
var i=1;
while (i>0)
{
sum += 7;
if (sum>=depth)
break;
else
sum -= 2;
i++;
}
console.log(i);
}
+ 2
function main() {
var depth = parseInt(readLine(), 10);
//your code goes here
//distance = distance covered
var distance=0;
var days=1;
while(distance<depth)
{
distance += 7;
if(distance<depth)
{
distance-=2;
days++;
}
}
console.log(days);
}
+ 1
But why ? Initial 1 day/7 feet not 0 ?
+ 1
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!!!
+ 1
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);
}
+ 1
climb =0
for(day=1;climb <depth;day++){
if (climb <depth){
climb =climb +7;
if (climb >=depth){
document.write(day);
}
else{
climb =climb -2;
}
}
}