我正在接近从Ruby背景学习JavaScript,所以我在理解(并将其置于文字中)时遇到了一些麻烦,为什么我的代码无法产生我需要的结果.我在pythontutor.com上运行了这个,看看发生了什么的逐步演练,这证实了我的怀疑.但是,我不确定为什么会这样.
我正在建造一个恒温器,一旦温度低于18℃,它应该返回"绿色".在我的倒数第二行,console.log是17这是正确的,但是当我thermostat.displayColor在最后一行调用它仍然说黄色.代码在那里终止,并且不会返回this.displayColor = this.currentColor()我期望它(因为它在第一次运行时执行此操作以将起始颜色定义为"黄色".
代码正常工作并返回'绿色'如果我改变代码直接调用原型方法this.currentColor(),但我只是想知道为什么它不让我按照我在下面写的方式来做.
我不确定描述这个问题的术语,所以提前道歉我的标题不准确.
var DEFAULT_TEMP = 20;
function Thermostat(){
this.temperature = DEFAULT_TEMP;
this.maxTemp = 25;
this.powerMode = 'on';
this.displayColor = this.currentColor()
};
Thermostat.prototype.downButton = function(){
if (this.temperature === 10){
throw new Error('temp cannot be lower than 10dC');
};
this.temperature --;
};
Thermostat.prototype.currentColor = function() {
if ((this.temperature >= 18) && (this.temperature < 25)) {
return 'yellow'
}
else if (this.temperature < 18) {
return 'green'
}
else {
return 'red'
} …Run Code Online (Sandbox Code Playgroud) React 新手,如果我的术语偏离目标,请提前道歉。我有一张桌子,上面列出了按特定顺序排列的人员名单。我需要能够this.props.tablePosition根据有关人员的索引创建一个值。
this.props是 Table 组件中的一个数组。我有下面的代码不起作用,因为我不确定如何获取索引。
<RankingCards {...this.props} tablePosition={this.props.indexOf(this)} />
Run Code Online (Sandbox Code Playgroud) 我有一个格式的多维列表:
list = [[1, 2, 3], [2, 4, 2], [0, 1, 1]]
Run Code Online (Sandbox Code Playgroud)
如何获取所有子列表的第三个值的最大值.在伪代码中:
max(list[0][2], list[1][2], list[2][2])
Run Code Online (Sandbox Code Playgroud)
我知道这可以通过迭代列表并将第三个值提取到一个新列表,然后简单地执行来完成max(list),但我想知道是否可以使用lambda或列表理解来完成这个?
我在我的数据库中存储的文档具有完全相同的"模式".每个文档都有三个浮点值(键是"A","B"和"C"),我需要做的是将每个文档输出为三个浮点数的平均值.
我还需要除以100,所以我的计算是(A + B + C)/ 3/100.据我所知,我需要将每个文档作为一个不同的输出进行投影,但$ group在这里没用(因为我没有对所有文档进行平均,只是每个文档).
我需要首先能够将其编写为MongoDB命令,然后将其转换为PHP.我想我将能够完成PHP部分,但我需要的是一些帮助开始实际的MongoDB命令...
db.measurements.aggregate( )
Run Code Online (Sandbox Code Playgroud)
我的数据存储如下:
{ "_id" : ObjectId(xxx), "A": 22.34, "B": 23.32, "C": 75.32, "device_id": 3 }
{ "_id" : ObjectId(xxx), "A": 22.34, "B": 23.32, "C": 75.32, "device_id": 3 }
{ "_id" : ObjectId(xxx), "A": 22.34, "B": 23.32, "C": 75.32, "device_id": 3 }
Run Code Online (Sandbox Code Playgroud)
当我提交get请求时,我需要我的数据如下:
{ "_id" : ObjectId(xxx), "Average of A,B,C": 40.50, "device_id": 3 }
{ "_id" : ObjectId(xxx), "Average of A,B,C": 40.50, "device_id": 3 }
{ "_id" : ObjectId(xxx), "Average of …Run Code Online (Sandbox Code Playgroud) 我在OSX上使用MongoDB,mongod确认正在运行.我检查了mongod.conf文件,并且存在以下内容并且未修改:
systemLog:
destination: file
path: /usr/local/var/log/mongodb/mongo.log
logAppend: true
Run Code Online (Sandbox Code Playgroud)
到现在为止还挺好.路径位置存在,但文件夹为空 - 没有mongo.log.这是一个标准安装,到目前为止一切都很好,除此之外.有关如何解决此问题的任何想法?
我在控制器中有两个操作方法,它们具有相同的重复代码,可以挽救两种不同类型的异常:
def wave
...do something here...
rescue ActionController::ParameterMissing => e
render :json => {:error => e.message}, :status => 422
rescue Vendor::ApiError => e
render :json => {:error => e.message}, :status => 500
end
def run
...do something different here...
rescue ActionController::ParameterMissing => e
render :json => {:error => e.message}, :status => 422
rescue Vendor::ApiError => e
render :json => {:error => e.message}, :status => 500
end
Run Code Online (Sandbox Code Playgroud)
能抢救处理程序只是被重构到一个共同的私有方法,并从两个叫run和wave方法呢?
我有一个方法,我试图在单元测试中消除它。真正的方法是用一个参数(一个字符串)调用的,然后发送一条文本消息。我需要删除该方法,但返回作为参数传入的字符串。
我在 RSpec 测试中的代码是这样的:
allow(taxi_driver).to receive(:send_text).with(:string).and_return(string)
Run Code Online (Sandbox Code Playgroud)
这将返回:
allow(taxi_driver).to receive(:send_text).with(:string).and_return(string)
Run Code Online (Sandbox Code Playgroud)
如果我将返回参数更改为:string,则会收到以下错误:
NameError: undefined local variable or method 'string'
Run Code Online (Sandbox Code Playgroud)
我尝试过谷歌搜索并检查 relishapp.com 网站,但找不到看起来非常简单明了的答案。
我在 Laravel Blade 中输出键值对列表时遇到问题。
我的 show.blade.php 模板是:
@extends('layouts.app')
@section('content')
<ul>
@foreach ($datapoint as $key => $value)
<li>{{ $key, $value }}</li>
@endforeach
</ul>
{{ $datapoint }}
@endsection
Run Code Online (Sandbox Code Playgroud)
我显示这个模板的控制器是:
<?php
namespace App\Http\Controllers;
use App\Hyfes;
use Illuminate\Http\Request;
class CaptainController extends Controller
{
public function getLiveData ()
{
$datapoint = Hyfes::find(1);
return view('captain.show')->with('datapoint', $datapoint);
}
}
Run Code Online (Sandbox Code Playgroud)
在浏览器中输出到视图的数据是这样的:
时间戳
{"date":"08/07/2017","time":"12:02:25","boat_name":"cyclone","current_location":"North Greenwich Pier","status":"停靠" ,"登船":26,"下船":2,"people_on_board":24,"current_speed":0,"hotel_power":231,"battery_power_demand":342,"battery_output_power":23,"engine_output_power":12," Battery_power_stored":100,"battery_depth_of_discharge":323,"fuel_consumption_rate":7,"nox":432,"co2":213,"battery_state_of_charge":100,"id":1}
理想情况下,我希望看到一个项目符号列表,例如:
我有两张地图:
a = %{ list: [1,2,3]}
b = %{ list: [4,5,6]}
Run Code Online (Sandbox Code Playgroud)
我希望合并/连接两个嵌套列表,结果是:
c = %{ list: [1,2,3,4,5,6]}
Run Code Online (Sandbox Code Playgroud)
我尝试过研究深度合并,但就我而言,我有列表而不是嵌套映射。
我的方法目前是这样的:
def calculate
bill = 0
order.each {|k, v| bill += restaurant.show_menu[k]*v}
bill
end
Run Code Online (Sandbox Code Playgroud)
它看起来真的很难看,但我需要它才能回归bill.考虑制作bill一个实例变量,然后在我的对象初始化时定义它,但我不在其他任何地方使用它.什么是重构这个的最好方法?