Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I deal with SyntaxError: Unexpected token

I want to know the logic of my error and how should I write it instead, please. Thank You

This is the question:

Write a function called "findShortestOfThreeWords".
Given 3 strings, "findShortestOfThreeWords" returns the shortest of the given strings.
If there are ties, it should return the first word in the parameters list.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The Error I get:

/home/runner/Module-1/index.js:21
}esle{
^
SyntaxError: Unexpected token ‘{‘

my code:

function findShortestOfThreeWords(word1, word2, word3){
  //if word1 was less than or equal word2
    //return word1
  //else
    //return word2
  //if word1 is less than or equal word3
    //return word1
  //esle 
    //return word3
  //if word2 is less than or equal word3
    //return word2
  //else
    //return word3

if(word1.length <= word2.length){
  return word1;
}else{
  return word2;
}if(word1.length <= word3.length){
  return word1;
}esle{
  return word3;
}if(word2.length <= word3.length ){
  return word2;
}else{
  return word3;
}
  
}

var output = findShortestOfThreeWords('a', 'two', 'three');
console.log(output); // --> 'a'

>Solution :

Change esle to else

function findShortestOfThreeWords(word1, word2, word3){
  //if word1 was less than or equal word2
    //return word1
  //else
    //return word2
  //if word1 is less than or equal word3
    //return word1
  //esle 
    //return word3
  //if word2 is less than or equal word3
    //return word2
  //else
    //return word3

if(word1.length <= word2.length){
  return word1;
}else{
  return word2;
}if(word1.length <= word3.length){
  return word1;
}else{
  return word3;
}if(word2.length <= word3.length ){
  return word2;
}else{
  return word3;
}
  
}

var output = findShortestOfThreeWords('a', 'two', 'three');
console.log(output); // --> 'a'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading