如何用javascript编写函数,将3的倍数转换为字符串

如何用javascript编写函数,将3的倍数转换为字符串,javascript,function,loops,if-statement,Javascript,Function,Loops,If Statement,这就是我到目前为止所做的,但是如何创建数字列表以及如何放入循环谢谢:只需使用一个包含数组的对象即可返回结果。 此外,必须在循环计数器中安装参数 函数mojitor(开始计数、结束计数){ 变量输出={ 门特:[], 玻璃:[], rhum:[], 薄荷糖:[], 莫吉托:[], }; 对于(var x=startOfCount;x我想你想要这样的东西: function mojitor(startOfCount, endOfCount) { var items = []; fo

这就是我到目前为止所做的,但是如何创建数字列表以及如何放入循环谢谢:

只需使用一个包含数组的对象即可返回结果。 此外,必须在循环计数器中安装参数

函数mojitor(开始计数、结束计数){
变量输出={
门特:[],
玻璃:[],
rhum:[],
薄荷糖:[],
莫吉托:[],
};

对于(var x=startOfCount;x我想你想要这样的东西:

function mojitor(startOfCount, endOfCount) {
    var items = [];
    for (var x=startOfCount; x <= endOfCount; x++){
        if( ( x % 3 == 0 ) && ( x % 5 == 0 )&& ( x % 7 == 0 ) ){
            items.push("Mojito");
        }
        else if( ( x % 3 == 0 ) && ( x % 5 == 0 ) ){
            items.push("MentheGlace");
        }
        else if( ( x % 3 == 0 ) && ( x % 7 == 0 ) ){
            items.push("MentheRum");
        }
        else if( x % 3 == 0 ){
            items.push("Menthe");
        }
        else if( x % 5 == 0 ){
            items.push("Glace");
        }
        else if( x % 7 == 0 ){
            items.push("Rhum");
        }
        else {
            items.push(x);
        }
    }
    return items;
}

var numbers = mojitor(1,110);

var line = "";
var j = 0;
for(i = 0; i < numbers.length; i++) {
    line += numbers[i] + " ";
    j++;
    if(j === 11){
        console.log(line);
        line = "";
        j = 0;
    }
}
if(line != ""){
    console.log(line); //prints the remaining
}
函数mojitor(开始计数、结束计数){
var项目=[];

对于(var x=startOfCount;x)就参数而言,我认为它们应该在您的
for
循环中使用:
for(var x=startOfCount;x我在您的问题描述中没有看到
“Mojito”
。在web上搜索“FooBar问题”。