我正在使用match和length属性检查字符串中的位数.这是我的函数http://codepen.io/PiotrBerebecki/pen/redMLE的codepen
最初在返回时numberOfDigits.length我收到一条错误消息(Cannot read property 'length' of null).我已经通过将行更改为来解决了这个问题(numberOfDigits && numberOfDigits.length).这有效,但我想更好地理解为什么现在可以执行新语句.解释器现在是否执行`numberOfDigits.length?
另外,为什么在操作数反转时我们会得到相同的错误(numberOfDigits.length && numberOfDigits)?
这是完整的JavaScript代码:
function checkLength(str) {
let numberOfDigits = str.match(/\d/g);
return (numberOfDigits && numberOfDigits.length);
}
console.log( checkLength('T3xt w1th sOme numb3rs') );
console.log( checkLength('Text with some numbers') );
Run Code Online (Sandbox Code Playgroud)
更新1: 以下答案解释说:
&&表达式中操作数的顺序计数.&&运算符,如果第一个操作数的计算结果为null,则JavaScript不会检查第二个操作数,因为表达式不能除了null/ 之外的其他值false.我正在尝试使用 Jest 和 Node.js 测试我的应用程序。使用 JestJS 运行测试时,避免在终端中出现以下错误的正确设置是什么?
无法读取 null 的属性“addEventListener”
sum一旦我注释掉在app.js文件中添加事件侦听器,该函数的测试就会通过。我什至不确定为什么这一行以及console.log('Not part...')Jest 执行的行,因为我只导出该sum函数。
我的 index.html 文件的内容:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<button id="button">JavaScript</button>
<script src="./app.js"></script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我的 app.js 文件的内容:
function sum(a, b) {
return a + b;
}
console.log('Not part of module.exports but still appearing in terminal, why?');
var button = document.getElementById('button');
button.addEventListener('click', function(e) {
console.log('button was clicked');
});
module.exports = {
sum
};
Run Code Online (Sandbox Code Playgroud)
我的 app.test.js 文件的内容:
var …Run Code Online (Sandbox Code Playgroud) 我试图在第二个ajax调用中使用第一个ajax调用的值.我正在使用下面的代码结构.出于某种原因,第二个调用返回undefined的userLocation variable.我怎么能重构我的代码,以便第一个ajax调用的userLocation值可以在第二个ajax调用的url中使用?
var userLocation;
function getUserLocation() {
$.ajax({
url: 'https://www.example.com/location.json',
success: function(response) {
userLocation = response.coordinates;
}
});
}
function getCurrentWeather() {
$.ajax({
url: 'https://www.example.com/weather' + userLocation + '.json',
success: function(response) {
console.log(response);
}
});
}
$(document).ready(function() {
$.when(
getUserLocation()
).done(
getCurrentWeather()
)
});
Run Code Online (Sandbox Code Playgroud)
更新1: 感谢下面提供的答案,我已经能够重构我的代码.现在,从第一个ajax调用接收的值可以在第二个ajax调用中使用.这是更新的代码:
var userLocation;
function getUserLocation() {
return $.ajax('https://www.example.com/location.json')
.then(function(response) {
return JSON.parse(response);
}).then(function(json) {
// data from the location.json file is available here
userLocation = json.coordinates;
})
}
function getCurrentWeather() { …Run Code Online (Sandbox Code Playgroud) 这个代码笔显示了我的问题:http ://codepen.io/PiotrBerebecki/pen/pNvpdG
当用户点击大按钮时,cssopacity减少到 0。因为我应用了以下规则:transition: opacity 0.5s ease-in-out;淡出动画是平滑的。
我想在下一个按钮淡入时实现相同的平滑过渡。但是由于某种原因,下一个按钮突然出现而没有任何过渡。
你知道是什么导致了这个问题以及如何解决它吗?
console.clear();
(function() {
// Data for the app
const model = {
buttons: ['tomato', 'blue'],
currentButton: -1
};
// Logic for the app
const controller = {
init: function() {
view.init();
},
getButtonName: function() {
model.currentButton = (model.currentButton + 1) % model.buttons.length;
return model.buttons[model.currentButton];
}
};
// View for the app
const view = {
init: function() {
this.root = document.getElementById('root');
this.showNext();
},
animationDelay: 500,
showNext: …Run Code Online (Sandbox Code Playgroud)我怎么能纠正下面的代码,以便它不返回函数的undefined值getPreciseLocation?
总之,当用户点击#precise-location-prompt并与浏览器共享位置时,应该有一个AJAX调用来获取当前天气.但是,此时undefined用户点击后会立即出现值错误#precise-location-prompt.
// Get weather data for the current location
function getCurrentWeather(coordinates) {
return $.ajax('http://www.example.com/lat=' + coordinates[0] + '&lon=' + coordinates[1]);
}
// Show weather in DOM
function showCurrentWeather(data) {
var currentTemperature = data.temperature;
$('#current-temperature').text(currentTemperature);
}
// Get precise location based via the Geolocation API
function getPreciseLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(displayPosition, displayError);
} else {
console.log("Geolocation is not supported by this browser");
}
}
// Success function for the …Run Code Online (Sandbox Code Playgroud) 这支笔显示我的问题:http://codepen.io/PiotrBerebecki/pen/RKxOKb
单击按钮时我可以更改背景颜色但是在单击时按住Shift键时我想应用不同的颜色.我当前的事件对象似乎无法访问键盘事件.
你知道怎么解决吗?
这是React代码:
class App extends React.Component {
constructor() {
super();
this.handleClick = this.handleClick.bind(this);
this.state = {
wasOriginalColorChanged: false,
buttonColor: 'blue'
}
}
handleClick(e) {
let newButtonColor;
if (!this.state.wasOriginalColorChanged) {
if (e.shifKey) {
newButtonColor = 'green';
} else {
newButtonColor = 'tomato';
}
} else {
newButtonColor = 'blue';
}
this.setState({
buttonColor: newButtonColor,
wasOriginalColorChanged: !this.state.wasOriginalColorChanged
});
}
render() {
return (
<div className="button"
onClick={this.handleClick}
style={{backgroundColor: this.state.buttonColor}}>
<p>Click me to change color to tomato</p>
<p>Click me + …Run Code Online (Sandbox Code Playgroud)我已经开发了这个codepen(http://codepen.io/PiotrBerebecki/pen/qZjojV?editors=0010)试图解决以下JavaScript的问题:
给定非负整数,返回一个包含相反顺序的独立数字列表的数组.示例:348597 =>正确的解决方案应该是[7,9,5,8,4,3]
下面的函数显然不正确,因为它返回["7","9","5","8","4","3"] - 正确的顺序,但带引号.我怎么能修改它以便它返回[7,9,5,8,4,3]?
function digitize(n) {
var initialArray = (""+n).split('');
var reversedArray = [];
for (var i = initialArray.length - 1; i >= 0; i--) {
reversedArray[i] = initialArray.shift();
}
return reversedArray;
}
Run Code Online (Sandbox Code Playgroud)