首页 \ 问答 \ 功能未按预期工作(function isn't working as expected)

功能未按预期工作(function isn't working as expected)

我正在开发一个接受2个参数的函数, $base$top

我希望返回$base的功率,该功率将小于$top和exponent。

我期待的输出是$power=125$exp=3

我已经设法让$exp正确,但是我的6 $power正在上升,我无法弄清楚为什么它会超过$top

这是我的代码:

<?php
$base = 5;
$top  = 130;

function topPower($base, $top) {

    $exp = 0;
    while($power<$top) {
        $exp++;
        $power = pow($base, $exp);  
    } 

    return array($power, $exp-1);
}

$results = topPower($base, $top);
print_r ($results);
?>

I am working on a function that accepts 2 parameters, $base and $top

I am looking to return the power of the $base which would be less than $top and the exponent.

The outputs that i am expecting is $power=125 and $exp=3.

I've managed to get $exp correct, but my $power is coming up as 625 and I cannot figure out why it is going over $top.

Here is my code:

<?php
$base = 5;
$top  = 130;

function topPower($base, $top) {

    $exp = 0;
    while($power<$top) {
        $exp++;
        $power = pow($base, $exp);  
    } 

    return array($power, $exp-1);
}

$results = topPower($base, $top);
print_r ($results);
?>

原文:https://stackoverflow.com/questions/20476306
更新时间:2022-12-15 19:12

最满意答案

大多数单页应用程序将所有请求路由到根路径,并让前端路由器接管。 我怀疑这是你的应用程序发生了什么。

您的后端代码或任何服务器配置代码中是否有任何形式的请求重定向逻辑?

您可以做的是将一些您不希望前端路由接管的路径列入白名单,例如以/ api开头的路径。 在这里粘贴服务器端配置将很有帮助。

在您的服务器配置中,当inDevfalse ,您有一个app.get('*', ...)来捕获所有请求并使用静态单页面应用程序进行响应。 因此API请求也会给出相同的响应。 您需要在通配符*之前重构您的路由以匹配/api这里可以找到一些例子


Most single page applications route all requests to the root path and let the front end router take over. I suspect that is what is happening to your app.

Do you have any form of requests redirection logic in your back end code or any server configuration code?

What you can do is to whitelist some paths that you don't want front end routing to take over, such as those that start with /api. Pasting your server side config here will be helpful.

In your server config, when inDev is false, you have a app.get('*', ...) that catches all requests and responds with the static single page app. Hence API requests will also give the same response. You will need to restructure your routes to match /api before the wildcard *. Some examples can be found here

相关问答

更多
  • 大多数单页应用程序将所有请求路由到根路径,并让前端路由器接管。 我怀疑这是你的应用程序发生了什么。 您的后端代码或任何服务器配置代码中是否有任何形式的请求重定向逻辑? 您可以做的是将一些您不希望前端路由接管的路径列入白名单,例如以/ api开头的路径。 在这里粘贴服务器端配置将很有帮助。 在您的服务器配置中,当inDev为false ,您有一个app.get('*', ...)来捕获所有请求并使用静态单页面应用程序进行响应。 因此API请求也会给出相同的响应。 您需要在通配符*之前重构您的路由以匹配/api ...
  • 嗯,这不是真正的“卷曲请求”。 这是一个HTTP请求。 Curl只是用于通过命令行执行HTTP(和其他)操作的工具。 在你的HTTP请求中,我可以看到你正在使用axios.get() ,但是你正在尝试发布一个post请求(你有一个你想要发送的数据对象)。 所以你应该使用axios.post() 。 最好查看axios页面以查看HTTP帖子的语法,包括如何在帖子中包含数据和标题对象。 在回答你的第二个问题时,是的,你可以。 在你的第一个axios帖子的.then .then()部分,你可以使用响应做另一个ax ...
  • 管理通过更改我发布到的url( url:'/en/api/endpoint/' )修复它,因为显然是为了POST请求: 您通过POST调用此URL,但URL不以斜杠结尾,并且您设置了APPEND_SLASH。 在保持POST数据时,Django无法重定向到斜杠URL。 将表单更改为指向127.0.0.1:8000/en/api/endpoint/(注意斜杠),或在Django设置中设置APPEND_SLASH = False 之后我开始获得Forbidden 403 ,但是添加: from django.v ...