SEI-ATL / UNIT_2

This repo is for all things Unit 2
0 stars 3 forks source link

[unit 2] Challenge Problems | 11/13/2020 #16

Closed romebell closed 4 years ago

romebell commented 4 years ago

Challenge 1

Write a function moreDotLessDash(str) that takes in a string and returns the true if the string contains more dots ('.') than dashes ('-'), false otherwise.

Examples:

moreDotLessDash('2-D arrays are fun. I think.'); // => true
moreDotLessDash('.-.-.'); // => true
moreDotLessDash('.-'); // => false
moreDotLessDash('..--'); // => false
function moreDotLessDash(str) {

}
romebell commented 4 years ago

Challenge 2

Write a function opposingSums(array) that takes in an array of numbers and returns a new array containing half the number of elements. Assume the array contains an even number of elements.

The first element of the new array should be the sum between the 1st and last element of the original array. The second element of the new array should be the sum between the 2nd and 2nd to last element. The third element of the new array should be the sum between the 3rd and 3rd to last element... etc.

Examples:

const arr1 = [3, 2, 11, 4, 7, 9];
opposingSums(arr1); // => [ 12, 9, 15 ]
const arr2 = [1, 2, 3, 4]
opposingSums(arr2); // => [ 5, 5 ]
function opposingSums(array) {

}
romebell commented 4 years ago

🛑 Wait before posting code

nitishdayal commented 4 years ago
/**
 * Takes in a string and
 * returns the true if the string contains more dots ('.') than dashes
 * ('-'), false otherwise.
 * 
 * @param {string} str - A string with some dots and dashes
 */
const moreDotLessDash = (str) => {
  const dotsAndDashesCounter = str.split('')
    .filter(char => char === '.' || char === '-')
    .reduce((itr, val) => {
      if (val === '.') itr['.']++
      else itr['-']++

      return itr
    }, { '.': 0, '-': 0 })

  return dotsAndDashesCounter['.'] > dotsAndDashesCounter['-']
}

console.log(moreDotLessDash('2-D arrays are fun. I think.')) // => true
console.log(moreDotLessDash('.-.-.')) // => true
console.log(moreDotLessDash('.-')) // => false
console.log(moreDotLessDash('..--')) // => false
/**
 * Takes in an array of numbers
 * and returns a new array containing half the number of elements.
 * Works with arrays of even or odd length
 * 
 * @param {number[]} arrOfNums - An array of numbers
 */
const opposingSums = (arrOfNums) => arrOfNums.map((val, i, arr) => {
  let myVal = arr.lastIndexOf(val) !== arr.length - 1 ? val + arr[arr.length - 1] : val
  arr.pop()

  return myVal
}).filter(val => typeof val !== null || typeof val !== undefined)

const arr1 = [3, 2, 11, 4, 7, 9]
console.log(opposingSums(arr1)) // => [ 12, 9, 15 ]
const arr2 = [1, 2, 3, 4]
console.log(opposingSums(arr2)) // => [ 5, 5 ]
const arr3 = [1, 2, 3]
console.log(opposingSums(arr3)) // => [ 4, 2 ]
const arr4 = [0, 1, 2, 3, 0]
console.log(opposingSums(arr4)) // => [ 0, 4, 2 ]
tcgilbert commented 4 years ago

Challenge 1

function moreDotLessDash(str) {
    let chars = str.split('');
    let dots = 0;
    let dash = 0;
    for (let i = 0; i < chars.length; i++) {
        if (chars[i] === ".") dots++
        if (chars[i] === "-") dash++
    }
    return dots > dash;
}

Challenge 2

function opposingSums(arr) {
    let newArray = [];
    for (let i = 0; i < (arr.length / 2); i++) {
        newArray.push(arr[i] + (arr[arr.length - 1 - i]))
    }
    return newArray;
}
ruvvet commented 4 years ago
// challenge 1

function moreDotLessDash(str) {
  // count all dots
  let dot = str.split('').reduce((counter, e) => {
    if (e == '.') {
      counter++;
    }
    return counter;
  }, 0);

  // count all dashes
  let dash = str.split('').reduce((counter, e) => {
    if (e == '-') {
      counter++;
    }
    return counter;
  }, 0);

  // if dot >dash return true
  if (dot > dash) {
    return true;
  }
  // else false
  return false;
}

console.log(moreDotLessDash('2-D arrays are fun. I think.'));
console.log(moreDotLessDash('.-.-.'));
console.log(moreDotLessDash('.-'));
console.log(moreDotLessDash('..--'));

// challenge 2

function opposingSums(array) {
  //initialize new array
  const newarr = [];
  // for every element in array up until the middle
  for (let i = 0; i < array.length / 2; i++) {
    // check if the array - the array index == i
    if (array.length - (array.length - i) == i) {
      // if yes, push the sum of the first and last elements
      newarr.push(array[i] + array[array.length - i - 1]);
    }
  }
  return newarr;
}

const arr1 = [3, 2, 11, 4, 7, 9];
opposingSums(arr1);

const arr2 = [1, 2, 3, 4];
opposingSums(arr2);
richardleung1 commented 4 years ago
//challenge 1
function moreDotLessDash(str) {
    let dotCount = 0;
    let dashCount = 0;
    for (let i = 0; i < str.length; i++) {
        const element = str[i];
        if (element === '.') {
            dotCount++;
        } else if (element === '-') {
            dashCount++;
        } else {
            continue;
        }
    }
    if (dotCount > dashCount) {
        return true;
    } else {
        return false;
    }
};

//challenge 2
function opposingSums(array) {
    let newArray = []
    for (let i = 0; i < (array.length / 2); i++) {
        newArray.push(array[i] + array[array.length - (i + 1)]);
    }
    return newArray;
};

console.log(moreDotLessDash('2-D arrays are fun. I think.')); // => true
console.log(moreDotLessDash('.-.-.')); // => true
console.log(moreDotLessDash('.-')); // => false
console.log(moreDotLessDash('..--')); // => false

const arr1 = [3, 2, 11, 4, 7, 9];
console.log(opposingSums(arr1)); // => [ 12, 9, 15 ]
const arr2 = [1, 2, 3, 4]
console.log(opposingSums(arr2)); // => [ 5, 5 ]
JJURIZ commented 4 years ago
const moreDotLessDash = (str) => {
    let dotArray = [];
    let dashArray = [];
    let stringArray = str.split('');
    for (let i = 0; i < stringArray.length; i++) {
        if (stringArray[i] === ".") {
            dotArray.push(i);
        } else if (stringArray[i] === "-") {
            dashArray.push(i)
        }
    }
    return dotArray.length > dashArray.length ? true : false;
}

console.log(moreDotLessDash('2-D arrays are fun. I think.')); // => true
console.log(moreDotLessDash('.-.-.')); // => true
console.log(moreDotLessDash('.-')); // => false
console.log(moreDotLessDash('..--')); // => false
const opposingSums = (arr) => {
let finalArray = [];
let firstHalf = [];
let secondHalf = [];

for (let i = 0; i < arr.length/2; i++){
    firstHalf.push(arr[i])
}
for (let j = arr.length-1; j >= arr.length/2; j--) {
    secondHalf.push(arr[j])
}

for (let i = 0; i < firstHalf.length; i++) {
    finalArray.push(firstHalf[i] + secondHalf[i])
}
return finalArray;
}

const arr1 = [3, 2, 11, 4, 7, 9];
console.log(opposingSums(arr1)); // => [ 12, 9, 15 ]
const arr2 = [1, 2, 3, 4]
console.log(opposingSums(arr2)); // => [ 5, 5 ]
CoreyWilson319 commented 4 years ago

Challenge 1

function moreDotLessDash(str) {
    let array = str.split("")
    let dots = 0; 
    let dashs = 0; 
    for (let i = 0; i < array.length; i++){
        if (array[i] === ".") {
            dots += 1;
        }
        else if (array[i] === "-") {
            dashs += 1;
        }
    }
    if (dots > dashs) {
        return true
    } else {
        return false
    }
}

Challenge 2

function opposingSums(array) {
    let newArray = []
    newArray.push(array[0] + array[array.length -1])
    newArray.push(array[1] + array[array.length -2])
    newArray.push(array[2] + array[array.length -3])
    return newArray
}
zfinnan commented 4 years ago
function moreDotLessDash(str) {
    if (console.log(("str".match(/./g)||[]).length)) > (console.log(("str".match(/-/g)||[]).length)) {
        return true
    }
}

moreDotLessDash('...-')

let myArray = [3,4,11,4,7,9];

function opposingSums(array) {
    return [array[0] + array[array.length - 1]];
    return [array[1] + array[array.length - 2]];
    return [array[2] + array[array.length - 3]];
}

opposingSums(myArray);
jtreeves commented 4 years ago
function moreDotLessDash(str) {
    let array = str.split('')
    function countInArray(list, value) {
        let count = 0
        for (let i = 0; i < list.length; i++) {
            if (list[i] === value) {
                count++
            }
        }
        return count
    }
    let dots = countInArray(array, '.')
    let dashes = countInArray(array, '-')
    if (dots > dashes) {
        return true
    } else {
        return false
    }
}
alanavery commented 4 years ago
// Challenge 1
let moreDotLessDash = str => {
  let array = str.split('');
  let dots = array.filter(element => element === '.');
  let dashes = array.filter(element => element === '-');
  return dots.length > dashes.length;
};

// Challenge 2
let opposingSums = array => {
  let newArray = [];
  for (let i = 0; i < array.length / 2; i++) {
    newArray.push(array[i] + array[(array.length - 1) - i]);
  }
  return newArray;
};
fmuwanguzi commented 4 years ago
function moreDotLessDash(str) {
    if(str.split('.').length > str.splt('-').length ) {
        return true;
    }else{
        return false;
    } 

}
ABarranco95 commented 4 years ago

// 1st challenge

function moreDotLessDash(str) {
    if(str.split('.').length > str.split('-').length) {
        return true 
    } else return false
}

moreDotLessDash('2-D arrays are fun. I think.');
moreDotLessDash('.-.-.');
moreDotLessDash('.-');
moreDotLessDash('..--');

// 2nd challenge - incomplete
function opposingSums(array) {
    let half = array.length/2;
    array.splice(0, half)
    for(let i = 0, i < half; i++) {

}

const arr1 = [3, 2, 11, 4, 7, 9];

opposingSums(arr1);
AJStrizzy commented 4 years ago

function moreDotLessDash(str) {
    let arr = str.split("")
    let dotArr = []
    let dashArr = []

    for ( let i = 0; i < arr.length; i++) {
        if (arr[i] === '.') {
            dotArr.push(arr[i])
        } else if (arr[i] === '-') {
            dashArr.push(arr[i])
        }
    } if (dotArr.length > dashArr.length) {
        return true
    } else {
        return false
    }
} 

console.log(moreDotLessDash('this....is not-a-drill'))
AlexJBustillos commented 4 years ago

Did not get to finish second one

function moreDotLessDash(str) {
    let dot = str.search('.');
    let dash = str.search('-');
    if (str.dot > str.dash) {
        return true
    };
};
console.log(moreDotLessDash('2-D arrays are fun. I think.'));

function opposingSums(array) {

}
tuhoalyhuynh commented 4 years ago

function moreDotLessDash(string) {
    return string.match(/-/g)
}

function opposingSums (array) {
    let newArray = [];
    for (let i = 0; i < (array.length / 2); i++) {
        let sum = array[i] + array[(array.length - (i + 1))]
        newArray.push(sum);
    }
    return newArray;
}
tylerdance commented 4 years ago
function moreDotLessDash(str) {
    const dots = str.match(/./g).length;
    const dashes = str.match(/-/g).length;
    if (dots.length > dashes.length) {
        return true;
    } else {
        return false
    }
}
console.log(moreDotLessDash('...'));

function opposingSums(array) {
    const arr1 = [];
    arr1.push(array[0] + array[-1], array[1] + array[-2], array[2] + array[-2])
    return arr1
}
console.log(opposingSums([3, 2, 11, 4, 7, 9]));
canourrea23 commented 4 years ago
function moreDotLessDash(str){
    if ('.' > '-') 
    return true
} 
else  {
    return false
}
anonyymous1 commented 4 years ago
function moreDotLessDash(str) {
    let chars = str.split('');
    let dot = 0;
    let dash = 0;
    for (let i = 0; i < chars.length; i++) {
        if (chars[i] === ".") dot++
        if (chars[i] === "-") dash++
    }
    return dot > dash;
}

console.log(moreDotLessDash('2-D arrays are fun. I think.'));
console.log(moreDotLessDash('.-.-.'));
console.log(moreDotLessDash('.-'));
console.log(moreDotLessDash('..--'));

No Second one

codebypaul commented 4 years ago

Challenge 1

function moreDotLessDash(str) {
    myArray = str.split('')
    let dots = 0
    let dashes = 0
    for (let i = 0; i < myArray.length; i++){
        if (myArray[i] === '.'){
            dots++
        }else if (myArray[i] === '-'){
            dashes++
        }else{
            null
        }
    }
    if (dots > dashes){
        console.log(true);
    }else{
        console.log(false);
    }
}

Challenge 2

function opposingSums(array) {
    const newArray = []
    while (array.length > (array.length/2)){
        newArray.push(array[0] + array[array.length -1])
        array.pop()
        array.shift()
    }
    console.log(newArray);   
}
JaxonNarramore commented 4 years ago
function opposingSums(array) {
    let math = arr1[0] + arr1[5];
    console.log(math);
    let math2 = arr1[1] + arr1[4];
    console.log(math2);
    let math3 = arr1[2] + arr1[3];
    console.log(math3);
}
Swolepenguin commented 4 years ago
const arr1 =[2,4,6,8,10,12]
const results = [];
function newArr(){
    const results = [];
    for( var i=0; i < arr1.length; i++){
        results.push(arr1[i]);
    }
    return results;
}
console.log(results)
NikkiHmltn commented 4 years ago

//Challenge 1

function moreDotLessDash(str) {
   let dots = (str.split(".").length -1);
   let dash = (str.split("-").length -1);
   if (dots > dash) {
       console.log("true")
   } else if (dash > dots) {
       console.log("false")
   }
}

moreDotLessDash("..-")

//Challenge 2 (not working)

const arr2 = [1, 2, 3, 4]

function opposingSums(array) {
    let half = Math.ceil(array.length / 2);  
    let firstArray = array.splice(0, half)
    let secArray = array.reverse()

    for(let i=0; i < firstArray.length; i++) {
        sum += firstArray[i] + secArray[i]
    }
    return sum

}
opposingSums(arr2)
andrewemcmanus commented 4 years ago
function moreDotLessDash(str) {
  let dots = [];
  let dashes = [];
  let input = str.split("");
  for (let i = 0; i < str.length; i++) {
    let character = str[i];
    if (character == ".") {
      dots.push(".");
    } else if (character == "-") {
      dashes.push("-");
    };
    }
    if (dots.length > dashes.length) {
      return true;
    } else {
      return false;
    }
  };

still incorrect:

function opposingSums (array) {
  let newArray = [];
  let length = array.length / 2;
  if (array.length % 2 == 1) {
    let oddNumber = "error";
    return oddNumber;
  } else {
    for (i = 0; i < length; i++) {
      let newElement = array[i] + array[array.length - 1];
      newArray.push(newElement);
    }
    return newArray;
  }
};
const arr1 = [3, 2, 11, 4, 7, 9];
console.log(opposingSums(arr1));
edgerees commented 4 years ago
function moreDotLessDash(str) {
    firstArray = str.split('')
    let dots = 0
    let dashes = 0
    for (let i = 0; i < firstArray.length; i++){
        if (firstArray[i] === '.'){
            dots++
        }else if (firstArray[i] === '-'){
            dashes++
        }else{
            null
        }
    }
    if (dots > dashes){
        console.log(true);
    }else{
        console.log(false);
    }
}
sschneeberg commented 4 years ago
function moreDotLessDash(str) {
    let [dot, dash] = [0, 0];
    for (i = 0; i < str.length; i++) {
        if (str[i] === '-') {
            dash += 1;
        } else if (str[i] === '.') {
            dot += 1;
        }
    }
    return (dot > dash);
}
function opposingSums(array) {
    let sumArray = [];
    for (i = 0; i < array.length / 2; i++) {
        sumArray.push(array[i] + array[array.length - (i + 1)]);
    }
    return sumArray
}
lev-choubine commented 4 years ago
let yourString = "gho ---- ....."
let result
let dot= [];
let dash = [];
function moreDotThenString(string) {
res = string.split("");

res.forEach(function(e) 
{if(e === "."){
dot.push(e)
return dot;
}else if( e=== "-"){
dash.push(e)
return dash;
}});

if(dot.length > dash.length){
    result =true;
}else{
    result = false
} return result;
}

console.log(moreDotThenString(yourString));
console.log(dot)
console.log(dash)
const arr1 = [3, 2, 11, 4, 7, 9]

let newArr =[];
function opposingSums(arr){
    let l = arr.length/2;
    console.log(l);
   for( let i =0; i < l; i++){
       let first;
       first = arr.splice(1,1) 
       let last;
       last =arr.pop()
       console.log(first);
       console.log(last);
       let sum;
       sum = last+first[0];
       console.log(sum);
       newArr.push(sum)
   } 
}
opposingSums(arr1);
console.log(newArr);
canourrea23 commented 4 years ago
function opposingSums(array) {
  var arr1 = [];

  for (var i = 0; i < Math.floor(array.length / 2); i++) {
    var firstElement = array[i];
    var lastElement = array[(array.length - 1) - i];

    arr1.push(firstElement + lastElement);
  }

  return arr1;
}
romebell commented 4 years ago

Solutions

Challenge 1

console.log(moreDotLessDash('2-D arrays are fun. I think.')); // => true
console.log(moreDotLessDash('.-.-.')); // => true
console.log(moreDotLessDash('.-')); // => false
console.log(moreDotLessDash('..--')); // => false

function moreDotLessDash(str) {
    // Make two variables
    let dots = 0;
    let dashes = 0;
    const strArray = str.split('');

    // Iterate through string
    for(let i = 0; i < strArray.length; i++) {
        let char = strArray[i];
        // Check to see if a char is equal a dash or dot
        if (char === '.') dots++;
        else if (char === '-') dashes++;
    }

    // return true or false
    return dots > dashes;
}

Challenge 2

const arr1 = [3, 2, 11, 4, 7, 9];
console.log(opposingSums(arr1)); // => [ 12, 9, 15 ]
const arr2 = [1, 2, 3, 4]
console.log(opposingSums(arr2)); // => [ 5, 5 ]

function opposingSums(array) {
    let result = [];
    let half = array.length / 2;

    // Iterate through half of the array
    for (let i = 0; i < half; i++) {
        // Grab the first element
        let first = array[i]; // 2
        // Grab the last element
        let last = array[array.length - 1 - i]; // 3
        // Add those together
        let sum = first + last; // 5
        // Push the sum into the result array
        result.push(sum); 
    }

    return result; // [5, 5]
}