Javascript 为什么控制台声称数组未定义?
当我运行以下第一个代码变体时,控制台会按预期打印阵列。但是当我运行第二个变量时,控制台声明数组未定义。谁能解释一下原因吗Javascript 为什么控制台声称数组未定义?,javascript,Javascript,当我运行以下第一个代码变体时,控制台会按预期打印阵列。但是当我运行第二个变量时,控制台声明数组未定义。谁能解释一下原因吗 function tipCalculator(bill){ var parcentage; if (bill < 50){ parcentage = .2; }else if (bill >= 50 && bill < 200) { parcentage = .15; }else{ parcentage =
function tipCalculator(bill){
var parcentage;
if (bill < 50){
parcentage = .2;
}else if (bill >= 50 && bill < 200) {
parcentage = .15;
}else{
parcentage = .1;
}
return parcentage * bill;
};
var bills = [124 , 48, 205];
var tips = [tipCalculator(bills[0]),
tipCalculator(bills[1]),
tipCalculator(bills[2])];
console.log(tips)
功能提示计算器(账单){
var parcentage;
如果(账单<50){
面积=.2;
}否则,如果(账单>=50和账单<200){
面积=.15;
}否则{
面积=.1;
}
退回包裹*票据;
};
var票据=[124,48,205];
var tips=[tipCalculator(票据[0]),
tipCalculator(法案[1]),
tipCalculator(法案[2]);
console.log(提示)
功能提示计算器(账单){
百分之二十的风险值=票据*0.2;
var fifteenyPercent=账单*0.15;
var百分之十=票据*0.1;
如果(账单<50){
console.log('服务员将获得账单的20%,即'+
百分之二十);
}否则,如果(账单>=50和账单<201){
console.log('服务员将获得账单的15%,即'+
百分之十五);
}否则,如果(账单>200){
日志('服务员将得到账单的10%,即'+10%);
}否则{
log(“服务员不会得到任何小费”);
}
};
var票据=[124,48,205];
var tips=[tipCalculator(票据[0]),
tipCalculator(法案[1]),
tipCalculator(法案[2]);
console.log(提示)
在第二个代码中,tipCalculator函数缺少返回语句,因此tips数组没有正确填充。函数需要返回以下内容:
功能提示计算器(账单){
百分之二十的风险值=票据*0.2;
var fifteenPercent=票据*0.15;
var百分之十=票据*0.1;
如果(账单<50){
log(“服务员将得到账单的20%,即“+20%”);
回报率百分之二十;
}否则,如果(账单>=50和账单<201){
console.log(“服务员将获得账单的15%,即“+15%”);
返回百分之五十;
}否则,如果(账单>200){
日志(“服务员将得到账单的10%,即“+10%);
回报率百分之十;
}
}
var票据=[124,48,205];
变量提示=[
tipCalculator(票据[0]),
tipCalculator(法案[1]),
tipCalculator(票据[2])
];
控制台日志(tips);
tipCalculator
不返回任何内容。为什么我需要返回函数?我应该返回什么,您能解释一下吗?在第二个示例中,您正在向控制台打印一个数组,该数组的值来自一个不返回值的函数-因此该数组有3个未定义的值。zhulkov非常感谢。。。。非常感谢你。知道了。你是个天才
function tipCalculator (bill){
var twentyPercent = bill * 0.2;
var fifteenyPercent = bill * 0.15;
var tenPercent = bill * 0.1;
if (bill < 50 ) {
console.log ('Waiter will get 20% of the bill which is ' +
twentyPercent);
} else if ( bill >= 50 && bill < 201) {
console.log( 'Waiter will get 15% of the bill which is ' +
fifteenyPercent);
} else if ( bill > 200) {
console.log(' Waiter will get 10% of the bill which is ' + tenPercent);
} else{
console.log('Waiter won\'t get any tip' );
}
};
var bills = [124 , 48, 205];
var tips = [tipCalculator(bills[0]),
tipCalculator(bills[1]),
tipCalculator(bills[2])];
console.log(tips)
function tipCalculator(bill) {
var twentyPercent = bill * 0.2;
var fifteenPercent = bill * 0.15;
var tenPercent = bill * 0.1;
if (bill < 50) {
console.log("Waiter will get 20% of the bill which is " + twentyPercent);
return twentyPercent;
} else if (bill >= 50 && bill < 201) {
console.log("Waiter will get 15% of the bill which is " + fifteenPercent);
return fifteenPercent;
} else if (bill > 200) {
console.log(" Waiter will get 10% of the bill which is " + tenPercent);
return tenPercent;
}
}
var bills = [124, 48, 205];
var tips = [
tipCalculator(bills[0]),
tipCalculator(bills[1]),
tipCalculator(bills[2])
];
console.log(tips);