0

I tried to make a function that returns an array, the output should be "My name is Sarah Adam" but actually it does not return anything

/*global s:true*/
var m = 'My name is ';
function updateMsg(h) {
    "use strict";
    var el = m + h;
    s = ['Adam', 'Joseph'];
    return s;
}
var n1 = document.getElementById("msg");
n1.textContent = updateMsg("Sarah")[0];
1
  • 6
    you are returning s (the array) Commented Jun 1, 2016 at 21:13

5 Answers 5

3

you are returning s (the array) - I think you want to return the concatenated message. As in:

Updated to include variable last names

var m = 'My name is ';

function updateMsg(h, index) {
  "use strict";
  var el = m + h;

  // array of last names
  var s = ['Adam', 'Joseph'];

  return el + ' ' + s[index]; // return the concatenated string instead
}
var n1 = document.getElementById("msg");

n1.textContent = updateMsg("Sarah", 0); // invoke with param

// console log (confirmation)
console.log(updateMsg("Sarah", 0));
console.log(updateMsg("Meenah", 1));
<div id="msg">
  hw
</div>

Sign up to request clarification or add additional context in comments.

Comments

3

You could use currying to accomplish this. Just swap your brackets [0] for parentheses (0).

var m = 'My name is ';

function updateMsg(h) {
  var s = ['Adam', 'Joseph'];
  return function(index) { // return a function that concatenates when given index
    return m + h + " " + s[index];
  };
}

var messageMaker = updateMsg("Sarah");
console.log(messageMaker(0));
console.log(messageMaker(1));

2 Comments

'currying' or 'querying' ?
@ochi It's currying. Here's a small tutorial. It's a concept from functional programming.
1

I think you want to acces one element of the list of lastnames an pass the name. I have corrected your code and do something similar to what you want :

let m = 'My name is ',
  s = ['Adam', 'Joseph'],
  updateMsg = (h, i) => m + h + ' ' + s[i],
  n1 = document.getElementById("msg");
n1.textContent = updateMsg("Sarah", 0);
<p id="msg"></p>

Comments

1

Details are in the source in the comments.

SNIPPET

/*
Global variable: intro
*/
var intro = 'My name is ';

/*
Paramenter: who
*/
function updateMsg(who) {

  /* 
  Array of 2 Strings: guess 
  Named it guess because that's
  what I ended up doing with it
  */
  var guess = [' Shadey', ' Joseph'];

  /*
  Concated String: `hello`
  Since `intro` is global it's always accessible
  `who` is the parameter with value of "Slim"
  `guess[0]` = 'Shadey'
  */
  var hello = intro + who + guess[0];

  /*
  Output of `updateMsg()` function is `hello`
  which is "My name is Slim Shadey"
  */
  return hello;
}

/*
Reference the `output` element as `noLogic`
*/
var noLogic = document.getElementById("msg");

/*
Set `noLogic's` text to whatever `updateMsg()`
returns
`updateMsg()` parameter is "Slim"
*/
noLogic.textContent = updateMsg("Slim");
<output id="msg"></output>

Comments

0

I guess the intent is to return an array with el prepended to each of the names in s. So you need to loop through the array to create the new array.

var m = 'My name is ';
function updateMsg(h) {
  "use strict";
  var el = m + h;
  var s = ['Adam', 'Joseph'];
  return s.map(function(name) {
    return el + ' ' + name;
  });
}
var n1 = document.getElementById("msg");
n1.textContent = updateMsg("Sarah")[0];
<div id="msg">
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.