将函数打印到控制台时 concat is not defined 错误
concat is not defined error when printing function to console
当 运行 下面的代码时,我收到下面的错误,代码一直工作到最后一个函数发生此错误。代码是从开始的函数中选择一个随机数和随机单词,然后在最终函数中将它们连接成一个字符串,输出应该是这样的:7 happy cats jumping.
ReferenceError: concat 未定义
//function to generate and display to console Word 1 - Random Number.
function Word1(min, max) {
min = Math.ceil(1);
max = Math.floor(9);
return Math.floor(Math.random() * (max - min)) + min;
}
const Random_Number = Word1()
console.log(Random_Number);
//Function to generate and display to console Word 2 - Random Emotion.
function Word2(word)
{
return word[Math.floor(Math.random() * word.length)];
}
var word = ["Happiness", "Sadness", "Fear", "Disgust", "Anger", "Suprise"];
console.log(Word2(word));
//Function to generate and display to console Word 3 - Random plural noun.
function Word3(noun)
{
return noun[Math.floor(Math.random() * noun.length)];
}
var noun = ["Noun", "Monday", "Program", "Pizza", "Computer", "Suprise"];
console.log(Word3(noun)+ "s");
//Function to generate and display to console Word 4 - Random verb.
function Word4(verb)
{
return verb[Math.floor(Math.random() * verb.length)];
}
var verb = ["Accept", "Hope", "Jump", "Lend", "Relax", "Damage"];
console.log(Word4(verb));
//Function to create password one-line string.
function password(passWord)
{
return passWord[concat(Word1 + " " + Word2 + " " + Word3 + " " + Word4)];
}
var passWord = password();
console.log(password);
你需要这样写代码
function password(passWord)
{
return passWord[Word1.concat(Word2 + " " + Word3 + " " + Word4)];
}
您需要解决的问题很少
1) 如果你没有在函数 Word1
中使用 min
和 max
那么你为什么要将参数传递给它.
2) 您应该将 Word2(word)
、Word3(noun) + "s"
和 Word4(verb)
的结果存储在一个变量中,以便您可以在任何地方访问它在代码中。
3) 因为你是连接所有单词的结果,所以你可以使用 template literal as
`${Random_Number} ${w2} ${w3} ${w4}`
//function to generate and display to console Word 1 - Random Number.
function Word1(min, max) {
min = Math.ceil(1);
max = Math.floor(9);
return Math.floor(Math.random() * (max - min)) + min;
}
const Random_Number = Word1();
console.log(Random_Number);
//-------------------------------------------------------------------------
var word = ["Happiness", "Sadness", "Fear", "Disgust", "Anger", "Suprise"];
//Function to generate and display to console Word 2 - Random Emotion.
function Word2(word) {
return word[Math.floor(Math.random() * word.length)];
}
const w2 = Word2(word);
//-------------------------------------------------------------------------
var noun = ["Noun", "Monday", "Program", "Pizza", "Computer", "Suprise"];
//Function to generate and display to console Word 3 - Random plural noun.
function Word3(noun) {
return noun[Math.floor(Math.random() * noun.length)];
}
const w3 = Word3(noun) + "s";
//-------------------------------------------------------------------------
var verb = ["Accept", "Hope", "Jump", "Lend", "Relax", "Damage"];
//Function to generate and display to console Word 4 - Random verb.
function Word4(verb) {
return verb[Math.floor(Math.random() * verb.length)];
}
const w4 = Word4(verb);
//-------------------------------------------------------------------------
//Function to create password one-line string.
function password() {
return `${Random_Number} ${w2} ${w3} ${w4}`;
}
var generatedPassword = password();
console.log(generatedPassword);
我想进一步简化程序
const generateRandomNumber = (arr) => Math.floor(Math.random() * arr.length);
const Word1 = (min, max) => Math.floor(Math.random() * (max - min)) + min;
const Word2 = (word) => word[generateRandomNumber(word)];
const Word3 = (noun) => noun[generateRandomNumber(noun)];
const Word4 = (verb) => verb[generateRandomNumber(verb)];
const Random_Number = Word1(1, 9);
//-------------------------------------------------------------------------
var word = ["Happiness", "Sadness", "Fear", "Disgust", "Anger", "Suprise"];
const w2 = Word2(word);
//-------------------------------------------------------------------------
var noun = ["Noun", "Monday", "Program", "Pizza", "Computer", "Suprise"];
const w3 = Word3(noun) + "s";
//-------------------------------------------------------------------------
var verb = ["Accept", "Hope", "Jump", "Lend", "Relax", "Damage"];
const w4 = Word4(verb);
//-------------------------------------------------------------------------
//Function to create password one-line string.
const password = () => `${Random_Number} ${w2} ${w3} ${w4}`;
var generatedPassword = password();
console.log(generatedPassword);
你可以更简化哪个
const generateRandomNumber = (len) => Math.floor(Math.random() * len);
const Word1 = (min, max) => Math.floor(Math.random() * (max - min)) + min;
const getWord = (arr) => arr[generateRandomNumber(arr.length)];
const Random_Number = Word1(1, 9);
//-------------------------------------------------------------------------
var word = ["Happiness", "Sadness", "Fear", "Disgust", "Anger", "Suprise"];
const w2 = getWord(word);
//-------------------------------------------------------------------------
var noun = ["Noun", "Monday", "Program", "Pizza", "Computer", "Suprise"];
const w3 = getWord(noun) + "s";
//-------------------------------------------------------------------------
var verb = ["Accept", "Hope", "Jump", "Lend", "Relax", "Damage"];
const w4 = getWord(verb);
//-------------------------------------------------------------------------
//Function to create password one-line string.
const password = () => `${Random_Number} ${w2} ${w3} ${w4}`;
var generatedPassword = password();
console.log(generatedPassword);
这是最干净的代码
const generateRandomNumber = (min, max) =>
Math.floor(Math.random() * (max - min)) + min;
const getWord = (arr) => arr[generateRandomNumber(0, arr.length)];
const Random_Number = generateRandomNumber(1, 9);
//-------------------------------------------------------------------------
var word = ["Happiness", "Sadness", "Fear", "Disgust", "Anger", "Suprise"];
const w2 = getWord(word);
//-------------------------------------------------------------------------
var noun = ["Noun", "Monday", "Program", "Pizza", "Computer", "Suprise"];
const w3 = getWord(noun) + "s";
//-------------------------------------------------------------------------
var verb = ["Accept", "Hope", "Jump", "Lend", "Relax", "Damage"];
const w4 = getWord(verb);
//-------------------------------------------------------------------------
//Function to create password one-line string.
const password = () => `${Random_Number} ${w2} ${w3} ${w4}`;
var generatedPassword = password();
console.log(generatedPassword);
当 运行 下面的代码时,我收到下面的错误,代码一直工作到最后一个函数发生此错误。代码是从开始的函数中选择一个随机数和随机单词,然后在最终函数中将它们连接成一个字符串,输出应该是这样的:7 happy cats jumping.
ReferenceError: concat 未定义
//function to generate and display to console Word 1 - Random Number.
function Word1(min, max) {
min = Math.ceil(1);
max = Math.floor(9);
return Math.floor(Math.random() * (max - min)) + min;
}
const Random_Number = Word1()
console.log(Random_Number);
//Function to generate and display to console Word 2 - Random Emotion.
function Word2(word)
{
return word[Math.floor(Math.random() * word.length)];
}
var word = ["Happiness", "Sadness", "Fear", "Disgust", "Anger", "Suprise"];
console.log(Word2(word));
//Function to generate and display to console Word 3 - Random plural noun.
function Word3(noun)
{
return noun[Math.floor(Math.random() * noun.length)];
}
var noun = ["Noun", "Monday", "Program", "Pizza", "Computer", "Suprise"];
console.log(Word3(noun)+ "s");
//Function to generate and display to console Word 4 - Random verb.
function Word4(verb)
{
return verb[Math.floor(Math.random() * verb.length)];
}
var verb = ["Accept", "Hope", "Jump", "Lend", "Relax", "Damage"];
console.log(Word4(verb));
//Function to create password one-line string.
function password(passWord)
{
return passWord[concat(Word1 + " " + Word2 + " " + Word3 + " " + Word4)];
}
var passWord = password();
console.log(password);
你需要这样写代码
function password(passWord)
{
return passWord[Word1.concat(Word2 + " " + Word3 + " " + Word4)];
}
您需要解决的问题很少
1) 如果你没有在函数 Word1
中使用 min
和 max
那么你为什么要将参数传递给它.
2) 您应该将 Word2(word)
、Word3(noun) + "s"
和 Word4(verb)
的结果存储在一个变量中,以便您可以在任何地方访问它在代码中。
3) 因为你是连接所有单词的结果,所以你可以使用 template literal as
`${Random_Number} ${w2} ${w3} ${w4}`
//function to generate and display to console Word 1 - Random Number.
function Word1(min, max) {
min = Math.ceil(1);
max = Math.floor(9);
return Math.floor(Math.random() * (max - min)) + min;
}
const Random_Number = Word1();
console.log(Random_Number);
//-------------------------------------------------------------------------
var word = ["Happiness", "Sadness", "Fear", "Disgust", "Anger", "Suprise"];
//Function to generate and display to console Word 2 - Random Emotion.
function Word2(word) {
return word[Math.floor(Math.random() * word.length)];
}
const w2 = Word2(word);
//-------------------------------------------------------------------------
var noun = ["Noun", "Monday", "Program", "Pizza", "Computer", "Suprise"];
//Function to generate and display to console Word 3 - Random plural noun.
function Word3(noun) {
return noun[Math.floor(Math.random() * noun.length)];
}
const w3 = Word3(noun) + "s";
//-------------------------------------------------------------------------
var verb = ["Accept", "Hope", "Jump", "Lend", "Relax", "Damage"];
//Function to generate and display to console Word 4 - Random verb.
function Word4(verb) {
return verb[Math.floor(Math.random() * verb.length)];
}
const w4 = Word4(verb);
//-------------------------------------------------------------------------
//Function to create password one-line string.
function password() {
return `${Random_Number} ${w2} ${w3} ${w4}`;
}
var generatedPassword = password();
console.log(generatedPassword);
我想进一步简化程序
const generateRandomNumber = (arr) => Math.floor(Math.random() * arr.length);
const Word1 = (min, max) => Math.floor(Math.random() * (max - min)) + min;
const Word2 = (word) => word[generateRandomNumber(word)];
const Word3 = (noun) => noun[generateRandomNumber(noun)];
const Word4 = (verb) => verb[generateRandomNumber(verb)];
const Random_Number = Word1(1, 9);
//-------------------------------------------------------------------------
var word = ["Happiness", "Sadness", "Fear", "Disgust", "Anger", "Suprise"];
const w2 = Word2(word);
//-------------------------------------------------------------------------
var noun = ["Noun", "Monday", "Program", "Pizza", "Computer", "Suprise"];
const w3 = Word3(noun) + "s";
//-------------------------------------------------------------------------
var verb = ["Accept", "Hope", "Jump", "Lend", "Relax", "Damage"];
const w4 = Word4(verb);
//-------------------------------------------------------------------------
//Function to create password one-line string.
const password = () => `${Random_Number} ${w2} ${w3} ${w4}`;
var generatedPassword = password();
console.log(generatedPassword);
你可以更简化哪个
const generateRandomNumber = (len) => Math.floor(Math.random() * len);
const Word1 = (min, max) => Math.floor(Math.random() * (max - min)) + min;
const getWord = (arr) => arr[generateRandomNumber(arr.length)];
const Random_Number = Word1(1, 9);
//-------------------------------------------------------------------------
var word = ["Happiness", "Sadness", "Fear", "Disgust", "Anger", "Suprise"];
const w2 = getWord(word);
//-------------------------------------------------------------------------
var noun = ["Noun", "Monday", "Program", "Pizza", "Computer", "Suprise"];
const w3 = getWord(noun) + "s";
//-------------------------------------------------------------------------
var verb = ["Accept", "Hope", "Jump", "Lend", "Relax", "Damage"];
const w4 = getWord(verb);
//-------------------------------------------------------------------------
//Function to create password one-line string.
const password = () => `${Random_Number} ${w2} ${w3} ${w4}`;
var generatedPassword = password();
console.log(generatedPassword);
这是最干净的代码
const generateRandomNumber = (min, max) =>
Math.floor(Math.random() * (max - min)) + min;
const getWord = (arr) => arr[generateRandomNumber(0, arr.length)];
const Random_Number = generateRandomNumber(1, 9);
//-------------------------------------------------------------------------
var word = ["Happiness", "Sadness", "Fear", "Disgust", "Anger", "Suprise"];
const w2 = getWord(word);
//-------------------------------------------------------------------------
var noun = ["Noun", "Monday", "Program", "Pizza", "Computer", "Suprise"];
const w3 = getWord(noun) + "s";
//-------------------------------------------------------------------------
var verb = ["Accept", "Hope", "Jump", "Lend", "Relax", "Damage"];
const w4 = getWord(verb);
//-------------------------------------------------------------------------
//Function to create password one-line string.
const password = () => `${Random_Number} ${w2} ${w3} ${w4}`;
var generatedPassword = password();
console.log(generatedPassword);