Missing element in the function - javascript

This function should create and display an array with numbers. There is some element missing which prevents this array from displaying and so fulfilling the "console.log" assumptions:
function createArray(number) {
var newArray = [/*10*/];
for(var counter = 1; counter <= number; counter++) {
newArray.push(counter);
}
}
console.log("table with numbers up to 6 = " + createArray(6));
console.log("table with numbers up to 1 = " + createArray(1));
console.log("Testing negatives (should display an empty array) " + createArray(-6));
console.log("Testing 0 (should display an empty array) " + createArray(0));
Could you analyze this and provide some feedback, please?

You're missing return newArray; at the end of the function. If there's no return statement, the function returns undefined by default.
function createArray(number) {
var newArray = [/*10*/];
for(var counter = 1; counter <= number; counter++) {
newArray.push(counter);
}
return newArray;
}
console.log("table with numbers up to 6 = " + createArray(6));
console.log("table with numbers up to 1 = " + createArray(1));
console.log("Testing negatives (should display an empty array) " + createArray(-6));
console.log("Testing 0 (should display an empty array) " + createArray(0));

Well, there is nothing wrong with your function, just you are not returning the array created, just add:
return newArray at the end of the function, linke this:
function createArray(number) {
var newArray = [/*10*/];
for(var counter = 1; counter <= number; counter++) {
newArray.push(counter);
}
return newArray;
}

Related

How can I extract all contained characters in a String? [duplicate]

I have a string with repeated letters. I want letters that are repeated more than once to show only once.
Example input: aaabbbccc
Expected output: abc
I've tried to create the code myself, but so far my function has the following problems:
if the letter doesn't repeat, it's not shown (it should be)
if it's repeated once, it's show only once (i.e. aa shows a - correct)
if it's repeated twice, shows all (i.e. aaa shows aaa - should be a)
if it's repeated 3 times, it shows 6 (if aaaa it shows aaaaaa - should be a)
function unique_char(string) {
var unique = '';
var count = 0;
for (var i = 0; i < string.length; i++) {
for (var j = i+1; j < string.length; j++) {
if (string[i] == string[j]) {
count++;
unique += string[i];
}
}
}
return unique;
}
document.write(unique_char('aaabbbccc'));
The function must be with loop inside a loop; that's why the second for is inside the first.
Fill a Set with the characters and concatenate its unique entries:
function unique(str) {
return String.prototype.concat.call(...new Set(str));
}
console.log(unique('abc')); // "abc"
console.log(unique('abcabc')); // "abc"
Convert it to an array first, then use Josh Mc’s answer at How to get unique values in an array, and rejoin, like so:
var nonUnique = "ababdefegg";
var unique = Array.from(nonUnique).filter(function(item, i, ar){ return ar.indexOf(item) === i; }).join('');
All in one line. :-)
Too late may be but still my version of answer to this post:
function extractUniqCharacters(str){
var temp = {};
for(var oindex=0;oindex<str.length;oindex++){
temp[str.charAt(oindex)] = 0; //Assign any value
}
return Object.keys(temp).join("");
}
You can use a regular expression with a custom replacement function:
function unique_char(string) {
return string.replace(/(.)\1*/g, function(sequence, char) {
if (sequence.length == 1) // if the letter doesn't repeat
return ""; // its not shown
if (sequence.length == 2) // if its repeated once
return char; // its show only once (if aa shows a)
if (sequence.length == 3) // if its repeated twice
return sequence; // shows all(if aaa shows aaa)
if (sequence.length == 4) // if its repeated 3 times
return Array(7).join(char); // it shows 6( if aaaa shows aaaaaa)
// else ???
return sequence;
});
}
Using lodash:
_.uniq('aaabbbccc').join(''); // gives 'abc'
Per the actual question: "if the letter doesn't repeat its not shown"
function unique_char(str)
{
var obj = new Object();
for (var i = 0; i < str.length; i++)
{
var chr = str[i];
if (chr in obj)
{
obj[chr] += 1;
}
else
{
obj[chr] = 1;
}
}
var multiples = [];
for (key in obj)
{
// Remove this test if you just want unique chars
// But still keep the multiples.push(key)
if (obj[key] > 1)
{
multiples.push(key);
}
}
return multiples.join("");
}
var str = "aaabbbccc";
document.write(unique_char(str));
Your problem is that you are adding to unique every time you find the character in string. Really you should probably do something like this (since you specified the answer must be a nested for loop):
function unique_char(string){
var str_length=string.length;
var unique='';
for(var i=0; i<str_length; i++){
var foundIt = false;
for(var j=0; j<unique.length; j++){
if(string[i]==unique[j]){
foundIt = true;
break;
}
}
if(!foundIt){
unique+=string[i];
}
}
return unique;
}
document.write( unique_char('aaabbbccc'))
In this we only add the character found in string to unique if it isn't already there. This is really not an efficient way to do this at all ... but based on your requirements it should work.
I can't run this since I don't have anything handy to run JavaScript in ... but the theory in this method should work.
Try this if duplicate characters have to be displayed once, i.e.,
for i/p: aaabbbccc o/p: abc
var str="aaabbbccc";
Array.prototype.map.call(str,
(obj,i)=>{
if(str.indexOf(obj,i+1)==-1 ){
return obj;
}
}
).join("");
//output: "abc"
And try this if only unique characters(String Bombarding Algo) have to be displayed, add another "and" condition to remove the characters which came more than once and display only unique characters, i.e.,
for i/p: aabbbkaha o/p: kh
var str="aabbbkaha";
Array.prototype.map.call(str,
(obj,i)=>{
if(str.indexOf(obj,i+1)==-1 && str.lastIndexOf(obj,i-1)==-1){ // another and condition
return obj;
}
}
).join("");
//output: "kh"
<script>
uniqueString = "";
alert("Displays the number of a specific character in user entered string and then finds the number of unique characters:");
function countChar(testString, lookFor) {
var charCounter = 0;
document.write("Looking at this string:<br>");
for (pos = 0; pos < testString.length; pos++) {
if (testString.charAt(pos) == lookFor) {
charCounter += 1;
document.write("<B>" + lookFor + "</B>");
} else
document.write(testString.charAt(pos));
}
document.write("<br><br>");
return charCounter;
}
function findNumberOfUniqueChar(testString) {
var numChar = 0,
uniqueChar = 0;
for (pos = 0; pos < testString.length; pos++) {
var newLookFor = "";
for (pos2 = 0; pos2 <= pos; pos2++) {
if (testString.charAt(pos) == testString.charAt(pos2)) {
numChar += 1;
}
}
if (numChar == 1) {
uniqueChar += 1;
uniqueString = uniqueString + " " + testString.charAt(pos)
}
numChar = 0;
}
return uniqueChar;
}
var testString = prompt("Give me a string of characters to check", "");
var lookFor = "startvalue";
while (lookFor.length > 1) {
if (lookFor != "startvalue")
alert("Please select only one character");
lookFor = prompt(testString + "\n\nWhat should character should I look for?", "");
}
document.write("I found " + countChar(testString, lookFor) + " of the<b> " + lookFor + "</B> character");
document.write("<br><br>I counted the following " + findNumberOfUniqueChar(testString) + " unique character(s):");
document.write("<br>" + uniqueString)
</script>
Here is the simplest function to do that
function remove(text)
{
var unique= "";
for(var i = 0; i < text.length; i++)
{
if(unique.indexOf(text.charAt(i)) < 0)
{
unique += text.charAt(i);
}
}
return unique;
}
The one line solution will be to use Set. const chars = [...new Set(s.split(''))];
If you want to return values in an array, you can use this function below.
const getUniqueChar = (str) => Array.from(str)
.filter((item, index, arr) => arr.slice(index + 1).indexOf(item) === -1);
console.log(getUniqueChar("aaabbbccc"));
Alternatively, you can use the Set constructor.
const getUniqueChar = (str) => new Set(str);
console.log(getUniqueChar("aaabbbccc"));
Here is the simplest function to do that pt. 2
const showUniqChars = (text) => {
let uniqChars = "";
for (const char of text) {
if (!uniqChars.includes(char))
uniqChars += char;
}
return uniqChars;
};
const countUnique = (s1, s2) => new Set(s1 + s2).size
a shorter way based on #le_m answer
let unique=myArray.filter((item,index,array)=>array.indexOf(item)===index)

JavaScript - Trying to figure out why my average sum isn't working?

Trying to work out why my code block isn't working to calculate the average of my array. Hoping a fresh set of eyes can tell me why it's not doing the math's.
Can I also just add that "mark" is an array which doesn't hold any integers and the numbers are stored in the console as opposed to be stored in the array it's-self. I am trying to get it to work through user input, when the user input's the mark into an input box and take the values stored in the console.
function getTotal() {
let total = 0;
let count = 0;
let i = 0;
for (let i = 0; i < mark.length; i++) {
total += mark.length[i];
}
if (mark[i] !== undefined) {
//legit value//
count++;
total += mark[i];
}
let avg = total / count;
console.log(avg)
}
document.getElementById("result").innerHTML = " The Average is " + total;
document.getElementById("Average").addEventListener("click", getTotal);
Sum the numbers in the array, and then divide by the array's length. If the array's length is 0, return NaN (or throw an error):
function getAverage(arr) {
if (!arr.length) return NaN; // handler empty array case
let total = 0;
for (let i = 0; i < arr.length; i++) {
total += arr[i];
}
return total / arr.length;
}
console.log(getAverage([2, 2, 2]));
console.log(getAverage([10, 20, 33]));
console.log(getAverage([]));
When you want to use the method in your code, create an event listener:
document.getElementById("Average").addEventListener("click", function() {
const average = getAverage(mark);
document.getElementById("result").innerHTML = " The Average is " + average ;
});
Here is a working snippet
function getTotal(mark) {
let total = 0;
let count = 0;
for (let i = 0; i < mark.length; i++) {
total += mark[i];
count++;
}
let avg = total / count;
console.log(avg)
document.getElementById("result").innerHTML = " The Average is " + avg;
}
document.getElementById("Average").addEventListener("click", () => {
getTotal([90, 96, 100, 98])
});
<div id="result"></div>
<button type="button" id="Average">Get Total</button>
mark.length[i] should probably be mark[i], also the if statement is outside of your for loop so it only ever checks the last index.
Usually when you have a "get" method you should return something. Also not sure where mark comes from so I made this snippet below to help ya out.
Also "document.getElementById("result").innerHTML = " The Average is " + total;" should be inside the function.
let mark = [1,2,3,4,5]
function getTotal() {
let items = mark.filter(n => n !== undefined)
let total = items.reduce((sum, n) => sum + n, 0)
let average = total / items.length
document.getElementById("result").innerHTML = " The Average is " + average;
}
document.getElementById("Average").addEventListener("click", getTotal);
<buttton id="Average">Get Average</button>
<div id="result"></div>

Repeated Number count result in Ascending order

i was trying to get counted result in Ascending order
The following is my code
var NewArray = [1,4,5,2,1,3,4,2,4,5,6,4,2,1,7];
var SortNewArray = NewArray.sort();
var SortNewArrayLength = SortNewArray.length;
var prev = SortNewArray[0];
var count = 1;
for(var i =0; i<SortNewArrayLength; i++)
{
if(SortNewArray[i] == prev)
{
count++;
}
else
{
console.log(SortNewArray[i] + " comes " + count + " times ");
prev = SortNewArray[i];
count = 1;
}
}
this is output i am getting
whatever i marked in red color. i want those count in ascending order
Please anyone can help me out?
var NewArray = [1,4,5,2,1,3,4,2,4,5,6,4,2,1,7];
var counts = {};
// first collect the count of each number
NewArray.forEach( function(n){
if( counts[n] ){
counts[n] += 1;
}
else {
counts[n] = 1;
}
});
// now counts = { 1:3, 2:3, 3:1, 4:4, 5:2, 6:1, 7:1 }
// get the keys (unique numbers from NewArray) and sort them by their values (count)
var keys = Object.keys( counts ).sort( function(a, b){
return counts[a] > counts[b];
})
// print key:value pairs sorted by value
keys.forEach( function( n ){
console.log("%s comes %d times", n, counts[n] );
})
/*
3 comes 1 times
6 comes 1 times
7 comes 1 times
5 comes 2 times
1 comes 3 times
2 comes 3 times
4 comes 4 times
*/
http://jsfiddle.net/s384qq66/
Change var count = 1; to var count = 0; and change
console.log(SortNewArray[i] + " comes " + count + " times ");
to
console.log(SortNewArray[i-1] + " comes " + count + " times ");

Javascript: randomly pair items from array without repeats

I am trying to make a very basic "secret santa" generator as one of my first Javascript projects. I have searched for hours for a solution to this problem but so far nothing has worked that I have found.
I have an array of names which need paired to each other. I successfully have them pairing to each other, but right now someone can be drawn twice. I am pushing the randomly chosen names to another array but I can't find a way to check the randomly chosen names against the ones already chosen.
var names = ["Sean","Kyle","Emily","Nick","Cotter","Brian","Jeremy","Kimmy","Pat","Johnny"];
var used = [];
var picks = [];
if (names.length % 2 != 0) {
alert("You must have an even number of names. You currently have " + names.length + " names.");
}
for( var i = 0; i < names.length; i++){
var random = Math.floor(Math.random()*names.length)
if(names[random] == names[i]) {
names[random] = names[random++];
picks.push(names[i] + " gets " + names[random]);
used.push(names[random]);
} else {
picks.push(names[i] + " gets " + names[random]);
used.push(names[random]);
}
}
console.log("picked array: ")
for(var k=0; k<picks.length; k++) {
console.log(picks[k]);
}
console.log("used array: " + used);
Thank you in advance for any help.
Create two arrays with the names, shuffle them, and make sure you don't pick the same name from both arrays :
var names = ["Sean","Kyle","Emily","Nick","Cotter","Brian","Jeremy","Kimmy","Pat","Johnny"];
if (names.length % 2 != 0) {
alert("You must have an even number of names. You currently have " + names.length + " names.");
} else {
var arr1 = names.slice(), // copy array
arr2 = names.slice(); // copy array again
arr1.sort(function() { return 0.5 - Math.random();}); // shuffle arrays
arr2.sort(function() { return 0.5 - Math.random();});
while (arr1.length) {
var name1 = arr1.pop(), // get the last value of arr1
name2 = arr2[0] == name1 ? arr2.pop() : arr2.shift();
// ^^ if the first value is the same as name1,
// get the last value, otherwise get the first
console.log(name1 + ' gets ' + name2);
}
}
FIDDLE
I would suggest a different approach. Shuffle, split, and zip, no mutation:
var splitAt = function(i, xs) {
var a = xs.slice(0, i);
var b = xs.slice(i, xs.length);
return [a, b];
};
var shuffle = function(xs) {
return xs.slice(0).sort(function() {
return .5 - Math.random();
});
};
var zip = function(xs) {
return xs[0].map(function(_,i) {
return xs.map(function(x) {
return x[i];
});
});
}
// Obviously assumes even array
var result = zip(splitAt(names.length/2, shuffle(names)));
//^
// [
// [ 'Nick', 'Kimmy' ],
// [ 'Sean', 'Johnny' ],
// [ 'Kyle', 'Brian' ],
// [ 'Cotter', 'Pat' ],
// [ 'Emily', 'Jeremy' ]
// ]
There is a multitude of ways you can achieve this.
The fastest to code, but not necessarily the randomest is:
var names = ["Sean","Kyle","Emily","Nick","Cotter","Brian","Jeremy","Kimmy","Pat","Johnny"];
function getPicks(names) {
return names.slice(0).sort(function(){ return Math.random()-0.5 }).map(function(name, index, arr){
return name + " gets " + arr[(index+1)%arr.length];
});
}
getPicks(names);
This is not very random because the shuffling isn't very good and also because you get a single cycle each time. There can be no two cycles A->B->C->A D->E->D.
If you want it to have a random number of cycles of variable length, you can split the names array in several arrays and do the above for each of them, then concatenate the results (see elclanrs).
Finally, the last solution is for each person to pick a person at random and if it's the same one, simply pick again. If the last name remaining in both arrays is the same, simply swap it with another pair.
var names = ["Sean","Kyle","Emily","Nick","Cotter","Brian","Jeremy","Kimmy","Pat","Johnny"];
var a = names.slice(0);
var b = names.slice(0);
var result = [];
while (a.length > 1) {
var i = extractRandomElement(a);
var j = extractRandomElement(b);
while (i===j) {
b.push(j);
j = extractRandomElement(b);
}
result.push({ a:i, b:j });
}
if (a[0] === b[0]) {
result.push({ a:a[0], b:result[0].b });
result[0].b = a[0];
} else {
result.push({ a:a[0], b:b[0] });
}
var pairs = result.map(function(item){ return item.a + ' gets ' + item.b});
function extractRandomElement(array) {
return array.splice(Math.floor(Math.random()*array.length),1)[0];
}
I'm a tad late, but thought I'd throw my answer in here. It essentially does the same thing #adeneo's does, but it uses the same basic code as OP:
var names = ["Sean","Kyle","Emily","Nick","Cotter","Brian","Jeremy","Kimmy","Pat","Johnny"];
pickpool = names.slice(0); // Slice the array at the first element to copy it by value
var used = [];
var picks = [];
if (names.length % 2 != 0) {
alert("You must have an even number of names. You currently have " + names.length + " names.");
}
for( var i = 0; i < names.length; i++){
var random = Math.floor(Math.random()*pickpool.length)
if(names[random] == names[i]) {
// names[random] = names[random++];
picks.push(names[i] + " gets " + pickpool[random++]);
pickpool.splice(random++,1);
} else {
picks.push(names[i] + " gets " + pickpool[random]);
pickpool.splice(random,1);
}
}
console.log("picked array: ");
for(var k=0; k<picks.length; k++) {
console.log(picks[k]);
}
http://jsfiddle.net/SNJpC/
If you don't need to keep the original array you can remove the names as they get selected and each time you pick a name check that it isn't an empty string before pushing it to the next array.
Another consideration...
If you are trying to make a 'Secret Santa' generator, by using random method you can get the same pair next year, and next...
This is another solution where you get all the possible pairs (without repeating a name itself or a pair) for multiple years.
var names = ["Sean", "Kyle", "Emily", "Nick", "Cotter", "Brian", "Jeremy", "Kimmy", "Pat", "Johnny"];
if (names.length % 2 != 0) {
alert("You must have an even number of names. You currently have " + names.length + " names.");
} else {
const arr1 = names.slice()
let arr2 = names.slice();
let countDown = number => {
if (number === 1) {
return;
}
const last = arr2.pop([number - 1]);
arr2.unshift(last);
let pairs = [];
arr1.map(item => {
const index = arr1.indexOf(item);
pairs.push(`${arr1[index]} gets ${arr2[index]}`)
})
console.log(pairs)
return countDown(number - 1);
}
countDown(names.length)
}

ARRAY javascript indexOf

I have a multi-dimensional array. I need some sort of loop to randomly pick a string in the array and check to see if that string contains a character. If it doesn't, then repeat until it finds one.
I wrote a for loop that checks the array for the string, BUT it starts from 0 and works its way through the array until it finds it. I need it randomly pick a string in the array...any help?
jsFiddle --> http://jsfiddle.net/hz2MZ/1/
jQuery
$(document).ready(function() {
var myarr = [{"Name":"Bob", "Char":"134"},
{"Name":"Phil", "Char":"134"},
{"Name":"Jane", "Char":"1"},
{"Name":"Don", "Char":"4"},
{"Name":"Dan", "Char":"2"},
{"Name":"Jan", "Char":"12"},
{"Name":"Bill", "Char":"24"},
{"Name":"Sam", "Char":"14"},
{"Name":"Jake", "Char":"23"},
{"Name":"Ben", "Char":"3"}];
$('button').click(function() {
for(var i = 0; i < myarr.length; i++) {
if(myarr[i].Char.indexOf('2') !== -1) {
alert("Name: " + myarr[i].Name + "\nChar: " + myarr[i].Char);
return;
} else {}
}
});
});
My solution
$('button').click(function () {
var found = false;
while (!found) {
var randomIndex = Math.floor(Math.random() * myarr.length);
if (myarr[randomIndex].Char.indexOf('2') !== -1) {
alert("Name: " + myarr[randomIndex].Name + "\nChar: " + myarr[randomIndex].Char);
found = true;
} else {}
}
});
edit
Updated endless loop problem
$('button').click(function () {
var found = false;
var maxRandom = 20;
var currentRandom = 0;
while (!found && currentRandom < maxRandom) {
var randomIndex = Math.floor((Math.random() * myarr.length));
currentRandom++;
if (myarr[randomIndex].Char.indexOf('2') !== -1) {
alert("Name: " + myarr[randomIndex].Name + "\nChar: " + myarr[randomIndex].Char);
found = true;
} else {}
}
}
One approach is to copy the array, then randomly splice one member from the copy and check for the required value:
function randomValueByChar(arr, c) {
var a = arr.slice && arr.slice();
var i = a.length || 0;
var t;
while (i--) {
t = a.splice(Math.random() * i | 0, 1);
if (t[0].Char.indexOf(c) != -1) return t;
}
}
Another approach is to create an array of the indexes (e.g. [0,1,2,3…]), then randomly splice one value at a time and use it as an index to retrieve a value from the array. Both approaches will not have an infinite loop and will visit each member a maximum of once.

Categories

Resources