我试图将数组的每个元素与用户输入进行比较,以便在用户的输入与数组的任何元素不匹配时生成消息。我用来做的代码如下。
var guess_input;
var finished = false;
var colors = ["Aqua", "BurlyWood", "Cornsilk", "DarkGrey", "DarkRed", "Indigo", "LightGrey"];
while (!finished) {
guess_input = prompt("I'm thinking of one of these colors:\n\n" + "Aqua, BurlyWood, Cornsilk, DarkGrey, DarkRed, Indigo, LightGrey" + "\n\nWhat is the color I'm thinking of?");
finished = check_guess();
}
}
function check_guess() {
if (guess_input != colors[0] || guess_input != colors[1] || guess_input != colors[2] || guess_input != colors[3]) {
alert("Sorry, I don't recognize that color!\n\n" + "Please try again.");
return false;
}
}
这段代码的问题在于,如果我只从数组中选择一个元素,它就可以正常工作。但是当我使用' OR'操作员,它不起作用。有没有更好的方法来做到这一点?我是java脚本的新手。
谢谢!
答案 0 :(得分:1)
您可以使用logical AND运算符&&
,因为您需要检查要检查的所有颜色。
if (guess_input != colors[0] && guess_input != colors[1] && guess_input != colors[2] && guess_input != colors[3]) {
alert("Sorry, I don't recognize that color!\n\n" + "Please try again.");
return false;
}
对于正常工作的代码,您还需要返回true
找到找到的颜色。
var guess_input;
var finished = false;
var colors = ["Aqua", "BurlyWood", "Cornsilk", "DarkGrey", "DarkRed", "Indigo", "LightGrey"];
while (!finished) {
guess_input = prompt("I'm thinking of one of these colors:\n\n" + "Aqua, BurlyWood, Cornsilk, DarkGrey, DarkRed, Indigo, LightGrey" + "\n\nWhat is the color I'm thinking of?");
finished = check_guess();
}
function check_guess() {
if (guess_input != colors[0] && guess_input != colors[1] && guess_input != colors[2] && guess_input != colors[3]) {
alert("Sorry, I don't recognize that color!\n\n" + "Please try again.");
return false;
}
return true; // necessary, otherwise the function returns undefined, which is a falsy value
}

答案 1 :(得分:0)
您需要将guess_input
与colours
数组中的项进行比较。这是Array.prototype.some()
方法的完美工作,true
方法返回false
或callback
,具体取决于var test_guess = colours.some(function(color) {
/* 'color' = each item in 'colours' */
return color === guess_input;
});
if (test_guess) {
/* correct ..... */
} else {
/* incorrect .... */
}
中定义的条件。例如......
.some()
此处colours
开始遍历true
数组中的所有项,直到条件返回test_guess
。如果猜测与颜色匹配,变量true
将为false
,否则为protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
&& null != data) {
Uri selectedImage = data.getData();
Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
selectedImage);
cropIntent.setData(selectedImage);
cropIntent.putExtra("crop", "true");
cropIntent.putExtra("aspectX", 7);
cropIntent.putExtra("aspectY", 9);
cropIntent.putExtra("outputX", 450);
cropIntent.putExtra("outputY", 350);
cropIntent.putExtra("return-data", false);
startActivityForResult(cropIntent, RESULT_CROP_IMG);
} else if (requestCode == RESULT_CROP_IMG && resultCode == RESULT_OK && data != null) {
Uri cropedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
// Get the cursor
Cursor cursor = getContentResolver().query(cropedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imgDecodableString = cursor.getString(columnIndex);
cursor.close();
getSharedPreferences("main", MODE_PRIVATE).edit().putString(Const.CARD_IMAGE, imgDecodableString).apply();
photo.setImageBitmap(BitmapFactory.decodeFile(imgDecodableString));
}
} catch (Exception e) {
Toast.makeText(this, "Произошла ошибка", Toast.LENGTH_LONG)
.show();
}
}
。