Home > Enterprise >  why is the return statment returning the whole if statement?
why is the return statment returning the whole if statement?

Time:12-09

im doing a test on code wars and its asking me to return name 'plays banjo' if the name starts with 'r' or 'R'. i h ave the following code

const name = 'randy'

function areYouPlayingBanjo(name) {

  const firstLetter = name.split('', 1)
  if (firstLetter==='R' || firstLetter==='r'){
      return `plays banjo`
    }else{
      `does not play banjo`
    }
}

console.log(areYouPlayingBanjo(name))

whenever i run it, it just returns the if statement, not the value of the if statement. what am i doing wrong? why wont it return the 'return' part of the if statement instead of the whole thing?

CodePudding user response:

You have 3 issues, first is that split('',1) will return array with one element, not string, it will return ['r'] in this case, so to check correctly you need to do firstLetter[0].

The second issue is that you are missing return statement in else

The third issue mentioned in comment below by @Andy is that you are not really doing name 'plays banjo'

Here is the correct implementation:

const name = 'randy'

function areYouPlayingBanjo(name) {
  const firstLetter = name.split('', 1)[0]

  if (firstLetter === 'R' || firstLetter === 'r') {
    return name   ` plays banjo`;
  } else {
    return name   ` does not play banjo`;
  }
}

console.log(areYouPlayingBanjo(name))

Also as mentioned by @Andy, instead of doing const firstLetter = name.split('', 1)[0] you could just do const firstLetter = name[0]

CodePudding user response:

name.split('', 1) returns array. So, you should implement it like this

if (firstLetter[0] === 'R' || firstLetter[0] === 'r')

And this would be like

const name = 'randy'

function areYouPlayingBanjo(name) {
  const firstLetter = name.split('', 1)
  if (firstLetter[0] === 'R' || firstLetter[0] === 'r'){
      return `plays banjo`
    
  } else {
      return `does not play banjo` //return here as well
  }
}

console.log(areYouPlayingBanjo(name))

CodePudding user response:

  1. The return should be returning undefined not "the whole if statement" - unless codewars is doing something perculiar.

  2. split returns an array but you're not using the first element. So when you come to your condition none of the checks will match because you're trying to do ['r'] === 'r' which will always be false. Because strings are iterable objects you can just grab the first character like you would an array name[0]. If you then immediately coerce it to lowercase you can then shorten your if condition too.

  3. Make sure you return the name variable in the returned string. You're already using template strings so ${name} plays/doesn't play banjo.

function areYouPlayingBanjo(name) {

  const firstLetter = name[0].toLowerCase();

  if (firstLetter === 'r') {
    return `${name} plays banjo`;
  } else {
    return `${name} does not play banjo`;
  }

}

const name = 'randy';
console.log(areYouPlayingBanjo(name));

  • Related