---
# System prepended metadata

title: Untitled

---

### Javascript exercises

> 編號：CANDY-006
> 程式語言：JavaScript
> 題目：找出在數字陣列裡跟其它元素不一樣的值

```javascript=
// test inputs
const findDifferentTests = {
  test1: [1, 1, 1, 1, 3, 1, 1, 1],
  test2: [2, 2, 2, 4, 2, 2],
  test3: [8, 3, 3, 3, 3, 3, 3, 3],
};

const findDifferent = (numbers) => {
  // object to store counted numbers
  const counterObj = {};

  numbers.forEach((number) => {
    if (counterObj[number]) {
      counterObj[number] += 1;
    } else {
      counterObj[number] = 1;
    }
  });

  // find number that only appear once
  const oddNumber = Object.keys(counterObj).find(
    (key) => counterObj[key] === 1
  );
  return +oddNumber; // converts string to number
};
console.log(findDifferent(findDifferentTests.test1));
console.log(findDifferent(findDifferentTests.test2));
console.log(findDifferent(findDifferentTests.test3));
```

> 編號：CANDY-007
> 程式語言：JavaScript
> 題目：在某個數字陣列裡，可能藏有某個不合群的奇數或偶數，試著找出它！

```javascript=
// test inputs
const findSomeDifferentTests = {
  test1: [2, 4, 0, 100, 4, 11, 2602, 36],
  test2: [160, 3, 1719, 19, 11, 13, -21],
};

const findSomeDifferent = (numbers) => {
  const evenArray = [];
  const oddArray = [];

  numbers.forEach((number) => {
    if (number % 2 === 0) {
      evenArray.push(number);
    } else {
      oddArray.push(number);
    }
  });

  return evenArray.length === 1 ? evenArray[0] : oddArray[0];
};
console.log(findSomeDifferent(findSomeDifferentTests.test1));
console.log(findSomeDifferent(findSomeDifferentTests.test2));
```

> 編號：CANDY-008
> 程式語言：JavaScript
> 題目：傳入一字串，計算得分最高的字
> 英文字母 a 得 1 分、b 得 2 分、c 得 3 分，以此類推。
> 所有傳入的字都是小寫。

```javascript=
// test inputs
const highestScoreWordTest = {
  test1: "lorem ipsum dolor sit amet",
  test2: "heyn i need a rubygem up to build this",
  test3: "in time machine there are some bugs",
};

// helper function for getting the corresponding alphabet number
// 'a' = 1, 'b' = 2, 'c' = 3, etc.
const getAlphabetNumber = (character) => {
  return character.charCodeAt() - 96;
};

const highestScoreWord = (str) => {
  let highestPoint = 0;
  let highestPointWord = "";

  // split the string
  const words = str.split(" ");

  // loop through all the words
  words.forEach((word) => {
    let total = 0;
    word.split("").forEach((letter) => {
      total += getAlphabetNumber(letter);
    });
    // check if word has higher points
    if (total > highestPoint) {
      highestPoint = total;
      highestPointWord = word;
    }
  });

  return highestPointWord;
};

console.log(highestScoreWord(highestScoreWordTest.test1));
console.log(highestScoreWord(highestScoreWordTest.test2));
console.log(highestScoreWord(highestScoreWordTest.test3));
```


> 編號：CANDY-009
> 程式語言：JavaScript
> 題目：移除網址中的錨點（Anchor）

```javascript=
const removeAnchorTests = {
  test1: "5xruby.tw",
  test2: "5xruby.tw/#about",
  test3: "5xruby.tw/courses/?page=1#about",
};

const removeAnchor = (url) => {
  const indexOfAnchor = url.indexOf("#");

  return indexOfAnchor === -1 ? url : url.substring(0, indexOfAnchor);
};

console.log(removeAnchor(removeAnchorTests.test1)); // 印出 5xruby.tw
console.log(removeAnchor(removeAnchorTests.test2)); // 印出 5xruby.tw/
console.log(removeAnchor(removeAnchorTests.test3)); // 印出 5xruby.tw/courses/?page=1
```