如何调用使用拆分函数创建的数组?

时间:2015-11-11 01:10:18

标签: javascript arrays function split

我正在进行编程挑战并且正在努力解决一个问题(主要观点是粗体)。

这是挑战:

        
  1. 首先,声明一个名为myArray
  2. 的数组     
  3. 大!现在用两个字符串填充myArray。将您的全名放在第一个字符串中,将Skype句柄放在第二个字符串中。
  4.     
  5. 接下来,声明一个名为cutName的函数。它应该以字符串作为参数。
  6.     
  7. cutName应该通过将输入字符串分解为单个单词来返回数组。例如" Douglas Crockford"应该以{{1​​}}返回。
  8.     
  9. 创建一个名为["Douglas", "Crockford"]的新空对象文字。     
  10. 按照以下准则向myData添加三个键值对:
  11.          6.1。 fullName:对myData中存储的名称字符串调用cutName
            6.2。 Skype:请参阅myArray中的Skype句柄         6.3。 github:如果你有一个github句柄,输入它作为一个字符串。如果不是,请将此值设为null     

    这是我的回答:

    myArray

    我在第6.1项上遇到了问题。

6 个答案:

答案 0 :(得分:0)

Call" cutName(myData.fullname)"



var myArray = ["Arthur Philadelpho", "arthurphiladelpho"];

function cutName (myArray){return myArray.split(" "); } 

var myData = {fullname:myArray[0], skype:myArray[1], github:null};


console.log(cutName(myData.fullname));

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您应该对名称字符串而不是整个数组执行拆分。

像这样:

myArray[0].split(" ")

这将返回一个包含两个单词的新数组。

这是完成的任务:

var myArray = [];

myArray.push('Pepito Perez');
myArray.push('pepitoperez');

function cutName (str) {
  return str.split(' ');
}

var myData = {
  fullname: cutName(myArray[0]),
  skype: myArray[1],
  github: 'http://ww.github.com/...' 
}

你有它,并不难。

代码说明

您的代码中存在一些问题:

/**
 * This part is fine, you are defining your initial array with two string in it.
 */
var myArray = ["Arthur Philadelpho", "arthurphiladelpho"];

/**
 * At this part you must create a function that takes an string to split it in
 * two parts and return an array with those part in it.
 * This function will return the splitted array once you call it, but in your 
 * code you never do that.
 * Also, you are taking an argument that is your original array and must be the
 * first Item of your array (your fullname), so you should call your function 
 * like this cutName(myArray[0]);, That way, you are passing the string to the 
 * function, instead of the whole array.
 * Inside the function you should split your string the way you are doing, but I
 * recomend you to change the argument name for something like 'str' for 
 * example, that way you wont get confused and won't be any change of shadowing
 * your code.
 */
function cutName (myArray){return myArray.split(" "); }

/**
 * Finally you have to call your function inside your object literal delaration
 * for the fullname key passing the first element of myArray as the function 
 * parameter like this "fullname: cutName(myArray[0])"
 */
var myData = {**fullname:myArray[0]**, skype:myArray[1], github:null};

答案 2 :(得分:0)

对此的快速回答是:

var myArray = ["Arthur Philadelpho", "arthurphiladelpho"];

function cutName (nameAndSurname){ return nameAndSurname.split(" "); }

var myData = { fullName: cutName(myArray[0])[0], skype:myArray[1], github:null};

<强>解释

cutName(myArray[0]) //=> returns ["Arthur", "Philadelpho"] as requested
cutName(myArray[0])[0] //=> access the first element on that array: "Arthur"

我建议你做一些这样的事情来使代码更清晰:

var myData = {};
myData.fullName = cutName(myArray[0])[0];
myData.skype = myArray[1];
myData.github = null;

答案 3 :(得分:0)

这是Hack Reactor挑战码:

var myArray=[];
myArray[0]="Rakib Hossain";
myArray[1]="rakib.csit";
function cutName(){
  
}
function cutName (myArray){return myArray.split(" "); }
var myData = {};
var myData = {
  fullName: cutName(myArray[0]),
  skype: myArray[1],
  github: null 
}

Admissions.showApp();

答案 4 :(得分:0)

// Your code here
// This Code should work
var myArray = [];
myArray[0] = "Prince Ndu";
myArray[1] = "Prynzdacypher";
function cutName(name) {
 return name.split(" ");
}
var myInfo =  {};
myInfo.fullName = myArray[0].split(" ");
myInfo.skype = myArray[1];
myInfo.github = null;

答案 5 :(得分:0)

这应该对您有用:

var myArray = []
myArray = (["Lynette Sandoval", "red"]);
function cutName(myArray) { return myArray.split(" "); }

var myInfo = {};

name = cutName(myArray[0]);      // calling cutname with myArray

Object.assign(myInfo, {
    fullName: name,
    favoriteColor: myArray[1],
    github: null
});