试图从函数内部推到外部 and/or 试图让我的函数在 console.log 中吐出一行
trying to .push from inside of a function to the outside and/or trying to make my function spit out a single line in console.log
我正在做这个作业:
Write a function named snapCrackle that takes one parameter: maxValue.
This function should loop through 1 up to maxValue (inclusive) and
build a string with the following conditions: If a number is odd,
concatenate "Snap, " to the end of the string. If a number is a
multiple of 5, concatenate "Crackle, " to the end of the string. If a
number is both odd and a multiple of 5 concatenate "SnapCrackle, " to
the end of the string. If a number is neither odd or a multiple of 5,
concatenate the number and ", " to the end of the string. This
function should console.log() the final string after maxValue
iterations of the loop.
这是我的解决方案:
let c = "Snap, ";
let i = "Crackle, ";
let x = "SnapCrackle, ";
let b = ",";
let maxValue = 20;
let counter = 1;
let output = "";
let list = ["snap", "crackle", "snapCrackle"];
function snapCrackle() {
while (counter <= maxValue) {
if (counter % 5 === 0 && counter % 2 !== 0) {
console.log(x + counter);
} else if (counter % 5 === 0) {
console.log(i + counter);
} else if (counter % 5 === 0 || counter % 2 !== 1) {
console.log(counter + b);
} else if (counter % 2 !== 0) {
console.log(c + counter);
}
counter++;
}
}
console.log();
return snapCrackle();
let results = " ";
console.log(snapCrackle());
有人告诉我:
Great start! You have everything you need, but it should be a single
string. For Snap, Crackle, and SnapCrackle, you shouldn't display the
number next to it, although I appreciate the cleverness and effort of
it. A hint I will give for solving this is creating an empty string
and concatenating the results to it!
如何将我的计数器推送到一个数组,以便 console.log
一行,或者如何让函数本身输出一行?
您已完成所有 hard-work。小改动,您的回答很好。
let c = "Snap";
let i = "Crackle";
let x = "SnapCrackle";
let maxValue = 20;
let counter = 1;
// let output = ""; // not required rather use an array
// let list = ["snap", "crackle", "snapCrackle"]; // not required
let outPutArr = [];
function snapCrackle() {
while (counter <= maxValue) {
if (counter % 5 === 0 && counter % 2 !== 0) {
outPutArr.push(x);
} else if (counter % 5 === 0) {
outPutArr.push(i);
} else if (counter % 5 === 0 || counter % 2 !== 1) {
outPutArr.push(counter);
} else if (counter % 2 !== 0) {
outPutArr.push(c);
}
counter++;
}
return outPutArr // return the created array from conditions
}
console.log(snapCrackle().join()); // Return the join of the array
我正在做这个作业:
Write a function named snapCrackle that takes one parameter: maxValue. This function should loop through 1 up to maxValue (inclusive) and build a string with the following conditions: If a number is odd, concatenate "Snap, " to the end of the string. If a number is a multiple of 5, concatenate "Crackle, " to the end of the string. If a number is both odd and a multiple of 5 concatenate "SnapCrackle, " to the end of the string. If a number is neither odd or a multiple of 5, concatenate the number and ", " to the end of the string. This function should console.log() the final string after maxValue iterations of the loop.
这是我的解决方案:
let c = "Snap, ";
let i = "Crackle, ";
let x = "SnapCrackle, ";
let b = ",";
let maxValue = 20;
let counter = 1;
let output = "";
let list = ["snap", "crackle", "snapCrackle"];
function snapCrackle() {
while (counter <= maxValue) {
if (counter % 5 === 0 && counter % 2 !== 0) {
console.log(x + counter);
} else if (counter % 5 === 0) {
console.log(i + counter);
} else if (counter % 5 === 0 || counter % 2 !== 1) {
console.log(counter + b);
} else if (counter % 2 !== 0) {
console.log(c + counter);
}
counter++;
}
}
console.log();
return snapCrackle();
let results = " ";
console.log(snapCrackle());
有人告诉我:
Great start! You have everything you need, but it should be a single string. For Snap, Crackle, and SnapCrackle, you shouldn't display the number next to it, although I appreciate the cleverness and effort of it. A hint I will give for solving this is creating an empty string and concatenating the results to it!
如何将我的计数器推送到一个数组,以便 console.log
一行,或者如何让函数本身输出一行?
您已完成所有 hard-work。小改动,您的回答很好。
let c = "Snap";
let i = "Crackle";
let x = "SnapCrackle";
let maxValue = 20;
let counter = 1;
// let output = ""; // not required rather use an array
// let list = ["snap", "crackle", "snapCrackle"]; // not required
let outPutArr = [];
function snapCrackle() {
while (counter <= maxValue) {
if (counter % 5 === 0 && counter % 2 !== 0) {
outPutArr.push(x);
} else if (counter % 5 === 0) {
outPutArr.push(i);
} else if (counter % 5 === 0 || counter % 2 !== 1) {
outPutArr.push(counter);
} else if (counter % 2 !== 0) {
outPutArr.push(c);
}
counter++;
}
return outPutArr // return the created array from conditions
}
console.log(snapCrackle().join()); // Return the join of the array