JavaScript函数,数组和对象

时间:2017-04-17 16:06:03

标签: javascript

这是我想要执行的JavaScript任务

  1. 首先,声明一个名为myArray的变量并将其分配给一个空数组。

  2. 大!现在用两个字符串填充myArray。 将您的全名放在第一个字符串中,将Skype句柄放在第二个字符串中。

  3. 接下来,声明一个名为cutName的函数。它应该是一个参数名称。

  4. cutName应该通过将输入字符串分解为返回数组 个别的话。例如" Douglas Crockford"应该归还为 [" Douglas"," Crockford"]

  5. 声明一个名为myInfo的新变量并将其分配给空对象 文字。

  6. 将以下三个键值对添加到myInfo:

    • 密钥: fullName 值:在名称字符串中调用cutName的结果 myArray的。

    • 密钥: skype: 值: myArray中的Skype句柄。

    • 密钥: github 值:如果您有github句柄,请在此处输入字符串。如果不, 将此设置为null。

  7. 这是我写的代码,我只得到4号程序错误

    var myArray = [];
    myArray = ["Safianu Mohammed", "mohammedsafianu"];
    
    function cutName(name){
       var fname = name;
       return  fname;
    }
    
    name = (cutName("Safianu Mohammed"));
    
    var myInfo = {};
    
    myInfo = {
       fullName: cutName(name), 
       skype: myArray[1],
       github: "null"
    };
    

5 个答案:

答案 0 :(得分:1)

var myArray = [];
myArray = ["Safianu Mohammed", "mohammedsafianu"];

function cutName(name){
    var fname = name;//missing a bit...
   return  fname;
 }

name = (cutName("Safianu Mohammed"));

var myInfo = {};

myInfo = {//reassigning = not your task
fullName: cutName(name), //why do you call cutName again??
skype: myArray[1],
github: "null"//not null
};

我将如何做到:

var myArray = [];
myArray.concat(["Safianu Mohammed", "mohammedsafianu"]);

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


name = cutName(myArray[0]);

var myInfo = {};

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

答案 1 :(得分:1)

我有这样的工作...

#define STATICALLY_ENFORCE_TYPES_NOT_COMPATIBLE(X,Y) \
    sizeof((char){_Generic((__typeof(X)*){0}, \
                  __typeof(__typeof(Y)*):(void)0,default:1)})

#define ARRAY_SIZEOF(X) \
   ((!STATICALLY_ENFORCE_TYPES_NOT_COMPATIBLE(X, &(X)[0]))?0:sizeof(X)) 

#define countof(X) (ARRAY_SIZEOF(X)/sizeof(*(X)))

答案 2 :(得分:0)

当您尝试使用Server Error in '/' Application. Value cannot be null. Parameter name: tooltip Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.ArgumentNullException: Value cannot be null. Parameter name: tooltip Source Error: An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. Stack Trace: [NullReferenceException: Object reference not set to an instance of an object.] Sitecore.Shell.Applications.ShellForm.OnLoad(EventArgs e) +2247 [TargetInvocationException: Exception has been thrown by the target of an invocation.] System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) +0 System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) +128 System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +146 Sitecore.Reflection.ReflectionUtil.InvokeMethod(MethodInfo method, Object[] parameters, Object obj) +89 Sitecore.Web.UI.Sheer.ClientPage.OnLoad(EventArgs e) +594 System.Web.UI.Control.LoadRecursive() +68 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4498Does 分割字符串时,不确定cutName函数如何在您的情况下起作用,但在您的名称中,全名中没有逗号。

错误的一个可能原因是您使用,上的 split功能。 Array功能可在split上使用,而不是String

所以,你需要使用

Array

而不是

var fname = myArray[0].split(", ");

var fname = myArray.split(", ");split上的一项功能,而不是String上的功能。

Array

答案 3 :(得分:0)

这是您要寻找的东西:

var myArray = [];
myArray = ["Ethan Halfhide", "red"];

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

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

console.log(myInfo);

答案 4 :(得分:0)

// Enter your code here
var myArray = []
myArray = (["west palmar","red"]);
function cutName (myArray){return myArray.split(" "); }

var myInfo = {};

name = cutName(myArray[0]);    

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