简体   繁体   中英

How can I solve this number doubling and halving problem?

I feel kind of dumb for asking this but I can't figure out this code challenge. I have to initialize a number with a value of one. Then I have to double this number till it's higher than 1000. After which it will be halved until it reaches 1 again, vice versa.

This is what I have so far. Obviously, it doesn't work as once it goes over 1000 and gets halved it will meet the requirement to get doubled again. I feel pretty stupid for not seeing the solution. Any help would be appreciated.

session_start();

$getal = 1;

if(isset($_SESSION['getal'])){
    if($_SESSION['getal'] <= 1000){
        $_SESSION['getal'] *= 2;
    }elseif ($_SESSION['getal'] > 1000){
        $_SESSION['getal'] *= 0.5;
    }
}
else{
    $_SESSION['getal'] = $getal;
}

You need to keep a "direction" in session.

Keep the multiplier to 2 until the value 1000 is reached. Then, change the multipler to.5 until you reach the value 1 (or less than 2).

session_start();

$getal = 1;

if (isset($_SESSION['getal'])) {
    if ($_SESSION['mult'] > 1 && $_SESSION['getal'] >= 1000) {
        $_SESSION['mult'] = .5;
    }
    elseif ($_SESSION['getal'] < 2) {
        $_SESSION['mult'] = 2;
    }

    $_SESSION['getal'] *= $_SESSION['mult'];
}
else{
    $_SESSION['getal'] = $getal;
    $_SESSION['mult'] = 2;
}

You need to store & check current direction (up/down), working code would be

<?php

session_start();

$getal = 1;

if (!isset($_SESSION['dir'])) {
    $_SESSION['dir'] = 'up';
}

if(isset($_SESSION['getal'])){
    if ($_SESSION['getal'] <= 1000 && $_SESSION['dir'] === 'up') {
        $_SESSION['getal'] *= 2;

        $_SESSION['dir'] = 'up';
    } else {
        $_SESSION['getal'] *= 0.5;

        $_SESSION['dir'] = 'down';

        if ($_SESSION['getal'] == 1) {
            $_SESSION['dir'] = 'up';
        }
    }
} else {
    $_SESSION['getal'] = $getal;
}

echo $_SESSION['getal'];
echo $_SESSION['dir'];

Your code is almost there, you just need to store the number you are multiplying by and check it, and you will be all good.

The basic idea is to store the multiplier as a session variable as well. You will need to change the multiplier whenever you get to 1 or above 1,000. I used a ternary statement to do this, because it is just assigning a variable value (which is what ternaries are for), but an if/then would work just as well.

<?php 

    session_start();

    if (isset($_SESSION['getal'])) {
        //the math
        $_SESSION['getal'] *= $_SESSION['multiplier'];
        //set the multiplier for the next iteration
        $_SESSION['multiplier'] = $_SESSION['getal'] > 1000 ? .5 : ($_SESSION['getal'] <= 1 ? 2 : $_SESSION['multiplier']);
    }
    else { 
        //initialize
        $_SESSION['getal'] = 1;
        $_SESSION['multiplier'] = 2;
    }

    echo $_SESSION['getal'];
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM